API Questions on Kentico API.
Version 6.x > API > Get Fields under Field Category View modes: 
User avatar
Member
Member
DahlinDev - 6/25/2012 11:28:25 PM
   
Get Fields under Field Category
How can I get a list of fields that are under a Field Category of a Document Type? I also want to get the list ordered in the same way they are ordered in the Document Type Fields definition.

User image

For more clarity in my screenshot I want to get:

Location
TimeOfDay
Weather

because they are under the "Attributes" category.

User avatar
Member
Member
kentico_michal - 6/27/2012 3:21:57 AM
   
RE:Get Fields under Field Category
Hi,

You can use the following code to access all fields definition including the category fields:

DataClassInfo dci = DataClassInfoProvider.GetDataClass("<class name>");
if (dci != null)
{
CMS.FormEngine.FormInfo fi = new CMS.FormEngine.FormInfo(dci.ClassFormDefinition);
if (fi != null)
{
// here you can access the fi.ItemsList collection which
// returns the list of all fields in the form of FormFieldInfos or FormCategoryInfos object.
}
}

Best regards,
Michal Legen

User avatar
Member
Member
DahlinDev - 6/27/2012 8:54:12 AM
   
RE:Get Fields under Field Category
This is exactly what I needed. Thank you!

For anyone else that needs this here is how I found what I needed for my column names.


List<String> attributes = new List<string>();
// here you can access the fi.ItemsList collection which
// returns the list of all fields in the form of FormFieldInfos or FormCategoryInfos object.
List<CMS.FormEngine.FormItem> formItems = fi.ItemsList;

int attributesCategoryIndex = formItems.FindIndex(i => i.ToString() == "CMS.FormEngine.FormCategoryInfo");
CMS.FormEngine.FormCategoryInfo ci = (CMS.FormEngine.FormCategoryInfo)formItems[attributesCategoryIndex];
if (ci.CategoryName == "Attributes")
{
for (int i = attributesCategoryIndex + 1; i < formItems.Count; i++)
{
attributes.Add(formItems.ToString());
}

}