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. I don't know what she was selling. How rude of me not to ask! 23 hours ago
  2. Muahaha, I just talked a telephone sales person into letting ME send THEM an invoice. She even gave me their org nbr and other details. 23 hours ago
  3. SL/MTR når nya höjder inom dålig informationsspridning idag. Tavlan säger en sak, megafonen en annan, personalen som skriker en tredje 2 days ago
follow me

Latest comments

  1. Martin S. wrote "Perfect stuff, that's just how to do it! Reminds me of an..." on Dear telephone sales woman
  2. Marcus Granström wrote "Poor girl. She didn't know what hit her when she called the ..." on Dear telephone sales woman
  3. Marthin wrote "Happy coding mate!" on New computer, anguish and excitement

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