Enumerating Posting and Channel properties

Have some of you have also been bothered by the fact that MCMS does not provide an enumeration method for Posting and Channel properties? Or for some of the other MCMS objects like Resources, Resource Galleries, Template or Template Galleries?

Such a feature would be pretty interesting if you (e.g.) create a generic server or user web control that should display the content of a given object.

Without an enumerator it would require to have a large switch-case expression. And in addition if the next service pack provides new properties you would have to adjust the control again to reflect the new properties.

Not very nice.

But the .NET framework itself provides a solution for this! It is named Reflection.

The following code will list all the properties including type and value of a given object:

public void WriteOutProperties(object o)

    foreach (PropertyInfo pi in o.GetType().GetProperties())
if (pi.PropertyType.ToString().StartsWith(“System”))
            Console.WriteLine(“pi: “+pi.Name+” — “+pi.PropertyType+” — “+pi.GetValue(o,
Console.WriteLine(“pi: “+pi.Name+” — “+pi.PropertyType+” — (no value for complex type)”);

The following code will return the value of a given property of an object (e.g. the “CreatedBy” property of a Posting)

public string PropertyByName(object o, string property)

    return o.GetType().GetProperty(property).GetValue(o,null).ToString();

Posting p = …;
Console.WriteLine(PropertyByName(posting, “CreatedBy”);

Using this method it is pretty easy to write such a generic user web or server control!

Comments (0)