Faking Dynamic Properties with PageTypeBuilder in EPiServer

When building websites in EPiServer you sometimes want a value of a property to be inherited from the parent page. Previously we used Dynamic Properties to have properties inherited, but when using PageTypeBuilder we are spolied with being able to code all our page type properties programactially in classes. However PageTypeBuilder doesn’t support creating Dynamic Properties, but that doesn’t stop us from creating the same functionality with ordinary properties.

After reading Joel’s blog post about dynamic properties with PageTypeBuilder and trying out the snippets of code he wrote, I decided to convert it into an extension method to make it easier to implement in more properties and projects.

Extension Method

public static TProperty GetInheritedProperty<TPageData, TProperty>(this TPageData pageData, Expression<Func<TPageData, TProperty>> expression)
    where TPageData : PageData
    where TProperty : class
    if (pageData == null) return null;
    if (expression == null) throw new ArgumentException("Missing expression parameter", "expression");

    TProperty value = pageData.GetPropertyValue(expression);

    if (value == null && pageData.ParentLink != null && pageData.PageLink != PageReference.StartPage)
        var page = DataFactory.Instance.GetPage(pageData.ParentLink) as TPageData;
        value = page != null ? expression.Compile()(page) : null;

    return value;

Sample Usage

[PageTypeProperty(Type = typeof(PropertyString), Tab = typeof(Information))]
public string MyInheirtedProperty
    get { return this.GetInheritedProperty(page => page.MyInheirtedProperty); }

If you have any improvements that can be made to this method – please do leave a comment.

published in PageTypeBuilder, ASP.NET, EPiServer