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;
}