Anders Hattestad
Sep 3, 2010
  7610
(1 votes)

PageTypeBuilder with Dynamic properties (updated)

I’m currently working on a project where we started using PageTypeBuilder after a refactoring process. We had some dynamic properties we needed to make available. I therefore made a hack around PTB that includes support for dynamic properties.

The concept is easy. In your top PageType base class you add an attribute. Then you tag up your properties but use Dynamic Property instead of PageTypeProperty

[DynamicPropertyContainer]
public class BasePageData : TypedPageData 
{
    [DynamicProperty]
    public string JavaScript { get; set; }
}

The code will find all class's tagged with DynamicPropertyContainer and find all properties in those classes tagged with DynamicProperty. If it finds 2 properties with the same name it will skip the last one :)

In front code you can of course use

<%=CurrentPage.JavaScript%>

I’,m using the version of PTB that is compailed for CMS 5, so it could maybe be some change in the CMS 6 version. But I needed to copy some code to my own project cause use of internal and private… (sorry couldn’t resist:) ). 

The code is available as 4 cs files, and 1 dll.

Quick Tip

Some times you need to access the property instead of the underline object that PageTypeBuilder returns. So if you make a class like this

public static class ClassInfo<T>
{
    public static string GetName<R>(Expression<Func<T, R>> expr)
    {
        var node = expr.Body as MemberExpression;
        if (object.ReferenceEquals(null, node))
            throw new InvalidOperationException("Expression must be of member access");
        return node.Member.Name;
    }
}

Then you can access the property like this

CurrentPage.Property[ClassInfo<BasePageData>(p=>p.JavaScript)]
Sep 03, 2010

Comments

Joel Abrahamsson
Joel Abrahamsson Sep 21, 2010 10:33 AM

Hi Anders!

You could use the GetPropertyName extension method that ships with PTB to accomplish what you do with the ClassInfo class.

It should also be added that while PTB doesn't support creating dynamic properties you can use it to access their values in a strongly typed way.

For more about why PTB doesn't support adding dynamic properties and how to use it to access their values see http://joelabrahamsson.com/entry/working-with-dynamic-properties-and-page-type-builder.

Sep 21, 2010 10:33 AM

Hi Joel

Thanks for the tip on GetPropertyName

The reason for this code is to ensure that the dynamic properties are created. That’s one of the huge benefits of PTB also. That’s way I used the same concept to create the properties.

If PTB should support dynamic properties is totally up to you, but I see benefit of automatically created dynamic properties. I have to say that I also like dynamic properties as a concept, and are not a huge fan of inherit properties thou the page tree.


/ Anders Hattestad

Please login to comment.
Latest blogs
Plug-in manager is back in CMS 12

Plug-in manager is back in the UI, what is it and how can i use it?

Luc Gosso (MVP) | Oct 6, 2022 | Syndicated blog

Display Child Pages in Content Delivery API Response

The below example will implement an instance of IContentConverterProvider to customise the serialisation of PageData and output child pages in the...

Minesh Shah (Netcel) | Oct 4, 2022

Bring the Report Center back in Optimizely CMS 12

The Report Center has been a part of Optimizely CMS since its first debut in version 5R2 in 2008, but in CMS 12, it's removed! Don't despair! Make...

Tomas Hensrud Gulla | Oct 4, 2022 | Syndicated blog

Customizing Property Lists in Optimizely CMS

Generic property lists is a cool editorial feature that has gained a lot of popularity - in spite of still being unsupported (officially). But if y...

Allan Thraen | Oct 2, 2022 | Syndicated blog

Optimizely names Luminary Senior Developer, Ynze Nunnink, OMVP

Luminary Senior Developer and Optimizely Lead, Ynze Nunnink has secured the coveted position of Optimizely MVP. Earning a Platinum badge for his...

Ynze | Oct 2, 2022 | Syndicated blog

Content Delivery API – The Case of the Duplicate API Refresh Token

Creating a custom refresh provider to resolve the issues with duplicate tokens in the DXC The post Content Delivery API – The Case of the Duplicate...

David Lewis | Sep 29, 2022 | Syndicated blog