How to add a LinkItemCollection property using Page Type Builder

Every now and then, often on Twitter, I get asked how to add a property of type LinkItemCollection to a page type using Page Type Builder. First of all, the solution is to use the PageTypeProperty attribute and specify the type of the property to be PropertyLinkCollection. The return type should be LinkItemCollection. Like this:

[PageTypeProperty(Type = typeof (PropertyLinkCollection)]
public virtual LinkItemCollection Links { get; set; }

More generally, when you use Page Type Builder you can use any type of EPiServer properties, including third party properties and your own custom properties. However, when you do so you must explicitly specify the type of (EPiServer) property that the (code) property should be mapped to, just like we did in the example above.

When you create properties of the standard (“system”) property types, such as PropertyNumber or PropertyBoolean that isn’t necessary as Page Type Builder will infer the property type from the code property’s return type. You can find a list of these default mappings in this post.

Of course, code properties of type LinkItemCollection could, and should, also be mapped to PropertyLinkCollection by default and I’ll fix that in the next release.

PS. For updates about new posts, sites I find useful and the occasional rant you can follow me on Twitter. You are also most welcome to subscribe to the RSS-feed.

Comments

  1. Anders Hattestad's avatar

    Anders Hattestad 1 years ago

    An other easy solution could be to change the Intercept method to actully check if the type is a PropertyData type and return that type like this:

    if (invocation.Method.ReturnType.IsSubclassOf(typeof(PropertyData))
        invocation.ReturnValue = page.Property[propertyName];
    else
        invocation.ReturnValue = page[propertyName];
    
    then you could declare it like this
    
    [PageTypeProperty()]
    public virtual PropertyLinkCollectionLinks { get; set; }
    
  2. Joel Abrahamsson's avatar

    Joel Abrahamsson 1 years ago

    Hmm, I don't see that solving actually adding the property to the page type?

  3. Anders Hattestad's avatar

    Anders Hattestad 1 years ago

    The problem as I see it, is that you need to know the underline value type of the property.

    If you could declare the PropertyData type as the property directly you don't need to know the type, and the removing the problem :)

  4. Joel Abrahamsson's avatar

    Joel Abrahamsson 1 years ago

    Hmm, no, what I was referring to was that PTB needs to know the type of the property (PageDefinition) to create for the page type. In other words, the problem isn't in retrieving the value.

Follow me on Twitter

  1. @tim_abell The gigantic ones are for customers who specifically ask for them only :) 1 months ago
  2. Looking to buy a gigantic easter egg filled with candy for delivery in Stockholm. Any recommendations? 1 months ago
  3. @strandberg_m Du måste skriva om resultatet efteråt! 1 months ago
follow me

Latest comments

  1. Joel Abrahamsson wrote "Hi Jonas! The fluent API is really geared towards working..." on Building a search page for an EPiServer site using Truffler
  2. Jonas wrote "Thank you for one more great write up! If you're not lucky ..." on Building a search page for an EPiServer site using Truffler
  3. David Knipe wrote "The CategoriesFacet method will save me a load of headaches ..." on Cool new features in the Truffler .NET API

About this site

This blog is built with EPiServer Community, EPiServer CMS, ASP.NET MVC and a bunch of other great products. The source code is available for download at the projects page, where you also can read more about this site and my other projects.

read more