Possible to identify Custom Fields?

Peter Karamitsos asked on November 5, 2019 23:07

I have added custom fields to the Contact object via the Admin interface:

Modules > Contact management > Classes > Contact > Fields

Now, in code, is there a way to know which fields are the custom fields only?

I know that in the database, it's always the last columns to be added, but is there a more robust way of determining this in code?

Recent Answers


Brenden Kehren answered on November 5, 2019 23:32

I believe you can check the cms_class tables ClassFormDefinition for the fields and check to see if that field is system or not. There may be a faster way in the api but that field has the values you need to look at even though it's all XML.

I beleive you can also set the property HideSystemFields of a basic form to true and it will only show the custom fields.

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on November 6, 2019 02:32 (last edited on November 6, 2019 03:39)

Yep, there is field is system but is only in custom modules :(. Image Text

So I guess naming convention like prefix i.e. acmeCustomField would work

FYI sql wise there are 2 fields where you can find fields definition:

select convert(xml, ClassFormDefinition), convert(xml, cast(ClassXmlSchema as varchar(max))) from cms_class where classname = 'OM.Contact'

but this is xml I would not go there, on top of that in SQL there is much easier way to get the columns:

`SELECT *  FROM   INFORMATION_SCHEMA.COLUMNS WHERE  TABLE_NAME = (select ClassTableName from cms_class where ClassName = 'Om.Contact')`

With Kentico API it is even more simple

    var myColumns = ClassStructureInfo.GetColumns("OM.Contact").Where(c => c.StartsWith("acme")).ToList();

P.S. You might as well dig into

var csi = ClassStructureInfo.GetClassInfo("OM.Contact");
var def = csi.ColumnDefinitions;

but I would say the naming convention is the way to to go.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.