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.
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
Comments
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; }Joel Abrahamsson 1 years ago
Hmm, I don't see that solving actually adding the property to the page type?
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 :)
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.