API
Version 7.x > API > Using API outside kentico View modes: 
User avatar
Certified Developer 10
Certified Developer 10
varinder-kudosweb - 6/19/2013 4:46:30 AM
   
Using API outside kentico
Hi

Im trying to build a .net mvc web api (which will work similar to kentico REST)
Followed docs (http://devnet.kentico.com/docs/devguide/index.html?using_api_and_cms_controls_outside_cms_project.htm) everything connects fine.

Id like to generate xml/json data containing all the documents (for the requested documenttype) with custom fields only.

For example,
- created a custom documenttype and called it "cms.foo"
- added two fields to cms.foo "name" and "description"
- created 10 cms.foo in the site tree.

Now-
- requesting "http://localhost/api/documenttype/cms.foo"

- In code - this is where im stuck -
 TreeProvider tp = new TreeProvider();
DataSet ds = tp.SelectNodes("CorporateSite", "/%", "en-us", true, "cms.foo", "", "", -1, true);

Im struggling how to filter that dataset down to its bare bones and generate a json out of it that looks something like this:
[
{
"cmsFooId": 1,
"name": "test",
"description": "test description"
},
{
"cmsFooId": 2,
"name": "test 2",
"description": "test description 2"
}
...
]

- Varinder

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 6/19/2013 6:35:59 AM
   
RE:Using API outside kentico
If all your looking for is those 3 columns then look into the overrides (12 of them) a bit further. Check this one out specifically
tp.SelectNodes("CorporateSite", "/%", "en-us", true, "cms.foo", "", "", -1, true, -1, "IdColumn,NameColumn,DescriptionColumn");
Of course you will have to use the column names in your cms.foo doc type.

User avatar
Certified Developer 10
Certified Developer 10
varinder-kudosweb - 6/19/2013 4:36:05 PM
   
RE:Using API outside kentico
Hi FroggEye,

That is very helpful.

One more question however,
Url now looks something like:
"http://localhost/api/documenttype/cms.foo?cols=idColumn,NameColumn,DescriptionColumn"
Which is kinda similar to what kentico REST provides

Is it possible (hope im not stretching it) to dynamically figure out which fields were created (ie, IdColumn, NameColumn, DescriptionColumn) and return data?

- Varinder

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 6/20/2013 6:41:07 AM
   
RE:Using API outside kentico
Via the REST API? I'm not sure. I know within the Kentico table CMS_Class there is a field which holds all of the properties (column names and datatypes) but with my inexperience with REST, I'm not sure if you can get that or not.

For a custom table item I have created this method that maps a datarows columns to a custom table rows columns and performs an insert by looking up the class information and getting the column information. This could also be used for a document type with some minor modifications.
/// <summary>
/// Creates an item for a custom table and dynamically adds the columns data based on the classinfo and row column comparison
/// </summary>
/// <param name="Dci"></param>
/// <param name="Row"></param>
/// <returns></returns>
public static bool CreateCustomTableItem(DataClassInfo Dci, DataRow Row)
{
bool okInsert = false;

// Creates new Custom table item provider
CustomTableItemProvider customTableProvider = new CustomTableItemProvider(ui);

if (Dci == null)
{
throw new Exception("Data class info cannot be null.");
}
else
{
// Creates new custom table item object
CustomTableItem newCustomTableItem = CustomTableItem.New(Dci.ClassName, customTableProvider);

List<string> classProperties = new List<string>();
// get the class definition
var doc = XDocument.Parse(Dci.ClassFormDefinition);
// get a list of all the tables properties by column value
List<string> elements = doc.Descendants("field").Select(el => el.Attribute("column").Value).ToList();

// got the class properties now compare
foreach (string s in elements)
{
// see if the class info contains the column in the data row passed in this method
if (Row.Table.Columns.Contains(s))
{
// contains the column so set a value
newCustomTableItem.SetValue(s, Row);
okInsert = true;
}
}

if (okInsert)
{
// Inserts the custom table item into database
newCustomTableItem.Insert();
}
}
return okInsert;
}

User avatar
Member
Member
richie-app.rainconcert - 3/24/2014 6:13:19 AM
   
RE:Using API outside kentico
Hi,

I'm a newbie to Kentico. I would like to create a custom Web API similar to the REST api like you had mentioned. I've gone through all the developer notes, and still find it hard on figuring out where to start. I would be really grateful if you could share your api folder, or guide me on how to start.