Portal Engine Questions on portal engine and web parts.
Version 7.x > Portal Engine > Programatic Access to Document Type Fieds View modes: 
User avatar
Certified Developer 13
Certified Developer 13
jay@3degreesnorth.com.au - 11/15/2013 2:09:09 AM
   
Programatic Access to Document Type Fieds
I need to write some code that will iterate through the fields added to a custom Document type. I know how I could do this by querying the database, however I'm hoping someone can point me at the API library I would use to do this?

I'll know the name of the document type - so just need to know the field names.

Thanks

Jay

User avatar
Kentico Customer Success
Kentico Customer Success
kentico_martind2 - 11/15/2013 8:17:51 AM
   
RE:Programatic Access to Document Type Fieds
Hello Jay,

I'm not sure if I understand your request correctly... You have a custom document type with custom fields and you want to get the names of these fields in the code (you don't know them?)?

Best regards,
Martin Danko

User avatar
Kentico Legend
Kentico Legend
Accepted solutionAccepted solution
Brenden Kehren - 11/15/2013 11:20:37 AM
   
RE:Programatic Access to Document Type Fieds
Jay,

I use this for creating custom table records. Esentially the same code will work for a doc type, just need to use the correct provider. The DataClassInfo object holds all the information about the document type, custom table, etc. The fields are stored in the ClassFormDefinition field/property. If you run a query against the cms_class table and look at this field it will have xml for all the fields, their datatypes, etc in there. I simply use Linq To XML to get that data into a list and iterate through each field. The 2 lines of code that do a lot of the magic and gets the columns info are here
// 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();

Full code block below

How to call it
DataClassInfo dci = DataClassInfoProvider.GetDataClass("custom.CustomTable");
return CreateCustomTableItem(dci, dataRow);
dataRow is any row from a dataTable. I use this because the column names in the datatable are named the same as the column names in the custom table, this allows me to dynamically add columns without modifying code.
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
Certified Developer 13
Certified Developer 13
jay@3degreesnorth.com.au - 11/16/2013 5:20:09 PM
   
RE:Programatic Access to Document Type Fieds
Hi FroggEye

This did the trick - thanks you your help.

Jay