Can you find out in code whether a page type contains a certain property

Tracey Penberthy asked on March 7, 2018 13:53

Hi

I am trying to write some code to ascertain whether a page type contains a specified column name.

So for example I have a page type myWebsite.NewsPage and I want to know in code behind whether that page type contains a Field named Tags.

I thought I could use the following:

public static bool PageTypeHasColumn(string pageTypeClassName, string columnName)
{
    var pagetype = DocumentTypeHelper.GetDocumentTypeClasses()
        .WhereEquals("ClassName", pageTypeClassName).FirstOrDefault();

    if (null != pagetype)
    {
        if (pagetype.ContainsColumn(columnName))
        {
            return true;
        }
    }
    return false;
}

But on inspecting pagetype.ColumnNames no custom fields are listed in there.

Is there a way to do this?

Many Thanks Tracey

Correct Answer

Peter Mogilnitski answered on March 7, 2018 16:48

I think you can do it with pure SQL, i.e.

  1. select ClassTableName from cms_class where ClassName = '...' - get your class table name

  2. SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'TableName' AND COLUMN_NAME = 'ColumnName'

In one query could be something like:

SELECT *  FROM   INFORMATION_SCHEMA.COLUMNS WHERE  TABLE_NAME = (select ClassTableName from cms_class where ClassName = '...') AND COLUMN_NAME = 'ColumnName' 
1 votesVote for this answer Unmark Correct answer

Recent Answers


Trevor Fayas answered on March 7, 2018 14:02

Yup you need to look at the cms Class object's xml schema, in the cms_class table there is a column that has the xml with all that classes fields that you can use xpath to see if that field exists.

If you need a code sample I can get it to you when I'm at my desk next.

1 votesVote for this answer Mark as a Correct answer

Tracey Penberthy answered on March 7, 2018 15:47

Thanks Trevor

A code sample would be very much appreciated!

1 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on March 7, 2018 16:17

Here's a snippet from my CSV Import module (on the market place), with the CSV importer i have to list all of the fields a class has, so you can leverage this to do the same! I cut out some of the unimportant stuff.

    // Get fields
    DataClassInfo ClassObject = DataClassInfoProvider.GetDataClassInfo("Custom.MyClass");
    XmlDocument ClassFormXml = new XmlDocument();
    ClassFormXml.LoadXml(ClassObject.ClassFormDefinition);
    var PrimaryKeyFieldNode = ClassFormXml.SelectSingleNode("/form/field[@isPK='true']");
    string PrimaryKeyFieldName = "";
    if (PrimaryKeyFieldNode != null)
    {
        PrimaryKeyFieldName = PrimaryKeyFieldNode.Attributes["column"].Value;
    }


    // loop through fields, creating a Label and drop down for each
    foreach (XmlNode fieldNode in ClassFormXml.SelectNodes("/form/field"))
    {
        string FieldName = fieldNode.Attributes["column"].Value;
        string FieldCaption = (fieldNode.SelectSingleNode("./properties/fieldcaption") != null ? fieldNode.SelectSingleNode("./properties/fieldcaption").InnerText : FieldName);

        // These are usually fields that are automatically added by Kentico
        bool IsAutoField = false;
        if (FieldName == PrimaryKeyFieldName || FieldName.EndsWith("CreatedBy") || FieldName.EndsWith("ModifiedBy") || FieldName.EndsWith("CreatedWhen") || FieldName.EndsWith("ModifiedWhen") || FieldName.EndsWith("GUID"))
        {
            IsAutoField = true;
        }

        bool required = false;
        if (fieldNode.Attributes["alloweempty"] != null)
        {
            required = ValidationHelper.GetBoolean(fieldNode.Attributes["alloweempty"].Value, false);
        }

    }

This loops through the form fields, but you can adjust the SelectNodes xpath expression if you just want to find a specific field or what not.

1 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on March 7, 2018 16:57 (last edited on March 7, 2018 17:02)

Parsing XML is pretty crazy, I am pretty sure you were on the right track with DataClassInfo and DataClassInfoProvider i.e.

DataClassInfo myclass = DataClassInfoProvider.GetDataClassInfo(ClassID)
myclass.ContainsColumn("ColumnName")
1 votesVote for this answer Mark as a Correct answer

Tracey Penberthy answered on March 7, 2018 19:32

Thanks all really appreciate your help with this.

Peter I tried using the DataClassInfoProvider as you suggested but again the Column names list did not contain any custom columns. However your SQL solution worked a treat!

Thank you

1 votesVote for this answer Mark as a Correct answer

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