August 24, 2009

In one of my previous posts I listed a number of suggested improvements to the EPiServer property system. My number 1 wish (Just after wish 0) was that there would be a settings property on page definitions. While being nothing like a native solution with a dedicated interface would be, I thought I would show you a simple way to use the help text field to store settings for your EPiServer page properties. I’ve seen this approach being mentioned before around the web but I never got around to use it myself as I always found it somewhat tedious to manage these settings in the admin mode. Since starting to use the PageTypeBuilder by Joel Abrahamsson this has somewhat changed and now there is a possibility to define help texts through attributes in your code, making them much easier to manage.

Therefore I thought that I would share with you how I have implemented EPiServer property settings through the help text field.

April 19, 2009

I just wanted to follow up on my previous post on creating an ‘nice feel’ custom EPiServer property that I posted a few weeks ago. Got some great comments and thought I would follow up with an updated version of my example property.

April 2, 2009

With the latest versions of EPiServer, creating a custom property is not especially complex anymore and it has been covered in lotsofposts. There is one aspect of custom property creation that I think is lacking and that is using a real value type. With that I mean that the Value property of that PropertyCar should return a Car object and not the base type, for instance a String if you inherit from the LongString property for example. I know that I might be a bit picky with this but it gives the property just that extra nice feel. Even EPiServer with it’s latest creation LinkCollection doesn’t do this, instead Value returns the raw data string that is stored in the database.

So how do we get that nice feel? It is a bit harder than you would expect at a first glance. I’m going to walk you through how I created a multiple page type selector who’s Value property returns a PageTypeCollection. We’re going to inherit from PropertyLongString and save the GUID of the selected page types in a comma separated string. I won’t go into any detail around how to do the standard stuff like overriding CreatePropertyControl, others have gone into details around that already.

Since our main goal was to get the Value property to return a PageTypeCollection we need to override that. To make this easy we’ll let it be a wrapper around a separate property of PageTypeCollection type that handles the conversion to and from a string.

One key thing when working with collection is that you either need to track if the items in the collection changes or have a read-only collection with read-only items in it. The former is not always possible if you are working with a collection that you don’t have control over like in our case. The CreateGuidList and ParseGuidList are helper methods that serializes/deserializes a PageTypeCollection to the GUID string.

Then we need to ensure that if the LongString value changes or the SetDefaultValue is called, the page type property will be reloaded. If you don’t do this, using this property type for a Dynamic property will easily break inheritance. I also used the same technique in CreateWritableClone to ensure that it won’t use the same PageTypeCollection.

One important piece in the puzzle is to override the LoadData and SaveData methods. This is because the EPiServer data access layer doesn’t actually look at the value type but at the Type property so it will expect a string. We can make it easy for ourselves and just use the Value property of the base class since this is what the LongString property uses.