How To Create A Custom Episerver Property

Out of the box, Episerver provides a number of useful Properties we can use to decorate our Pages and Blocks, PropertyBoolean (Boolean),  (ContentArea), PropertyCategory (Category), PropertyContentReference (Content Reference)

What happens if you want to store data that doesn’t quite fit into one of the pre-existing properties?  In today’s tutorial, we’re going to go over how you can create your own custom property.  The property we are creating is going to be for the back-end storage purposes only.  I’m  going to skip how we would go about creating a front end view for it in the Episerver editor (the Dojo bit) for simplicity and to show properties are made from two parts, the data part and the front end GUI part.

.If you want to know how to attach a front-end display in the Episerver editor, then I would recommend  having a look at, Dummy’s Guide To Creating A Custom Dojo EPiServer Property

PropertyData

All Episerver properties need to inherit from ‘PropertyData’, which lives in the EPiServer.Core name in Episerver.dll.

PropertyData has a number of properties and method you can override.  If you want to create a new type, say a List<> then you will need to override the PropertyValueType, to get and set the properties data you need to override the Value property.

The clever thing about this process is that it allows you to create any type you want; the Value property takes an object so you can do whatever logic you want to in there.  The PropertyValueType then allows you to tell Episerver what type of object it is.

Creating an List Property

A very common scenario is to save data in a list format.  Unfortunately, Episerver can be a bit lacking in this area. If you want a way to store a list of string, you can easily use the snippet below:

[PropertyDefinitionTypePlugIn(DisplayName = "Int List")]
public class PropertyIntList : PropertyLongString
{
public override Type PropertyValueType
{
get
{
return typeof(List<int>);
}
}
public override object Value
{
get
{
var value = base.Value as string;
if (value == null)
{
return null;
}
return JsonConvert.DeserializeObject<IEnumerable<int>>(value);
}
set
{
if (value is IEnumerable<int>)
{
base.Value = JsonConvert.SerializeObject(value);
}
else
{
base.Value = value;
}
}
}
public override object SaveData(PropertyDataCollection properties)
{
return LongString;
}
public override IPropertyControl CreatePropertyControl()
{
return null;
}

As we’ll be creating a list of objects, we will need as much space as possible to store it against in the database. In this instance, the PropertyLongString will give us the most amount of space as it’s a NVarChar(Max).  I should probably warn you that with this solution, you will run out of space to save data against the row, so there is a limit to the  number of objects you can store in the list.  I have used this solution to store a good 20 odd objects so you should be safe in the majority of situations, but it is useful to know that there is a limit.

Next we override the PropertyValueType and return the type of item we want EpiSErver to cast to, in this case a List.

We then override the Value property. In the get, we use Json.Net to convert the item into a serialised string and save it.  If you want some more information about this, I have previously written about Json.Net in this article, How To Store Multiple Objects Against A LineItem Or The Cart In Episerver Commerce

In the set part of the property, we then de-serialize the string we stored against the property and return the List of Ints as an object.  It’s that simple.  You can obviously change this to create a list with whatever type you care about.

Conclusion

In today’s tutorial, we’ve talked about Episerver properties. Episerver properties are made of two parts, the PropertyData (the bits that store’s and retrieves the data to Episerver) and the property controls that display the data in the editor.  I haven’t included the property controls/display part in this tutorial to keep things simple, as Dojo is quite complex to get your head around if you’ve never touched it before.

All Episerver properties inherit from PropertyData.  In our example, we created a custom List<int) by returning a List

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *