Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

Per Magne Skuseth
Nov 2, 2015
  14699
(0 votes)

Trying out PropertyList<T>

Since EPiServer 9.0.0, EPiServer.Core.PropertyList<T> has been available through the API. Keep in mind that this is  a pre-release API, as documented in the code: NOTE: This is a pre-release API that is UNSTABLE and might not satisfy the compatibility requirements as denoted by its associated normal version.

What is PropertyList<T>?
The new PropertyList type allows you to add editable list properties to your content. The property value will be stored in the database as serialized JSON objects. Below I’ve written an example on how you can use the new property type, with a  simple “Contact” class, which will be stored as an IList<Contact> on a page type.

listedit

 

The models
A very simple contact class. You could add other property types to it as well, such as ContentReference!

public class Contact
{
    public string Name { get; set; }
    public int Age { get; set; }
    [Display(Name = "Phone number")]
    public int PhoneNumber { get; set; }
}

 

A page type with a list of contacts:

public class StartPage : PageData
{
    public virtual IList<Contact> Contacts { get; set; }
 
    // ...
}

 

Register the property definition
If you start the site at this point, you’ll get an error message, saying “Type '<Your type>' could not be mapped to a PropertyDefinitionType.”
Register the type by using the PropertyDefinitionTypePlugin.

[PropertyDefinitionTypePlugIn]
public class ContactListProperty : PropertyListBase<Contact>
{
 
}

 

The ContactListProperty could inherit PropertyList directly, but in this case I have created a base class, PropertyListBase, that can be reused by other types. This inherits the abstract class PropertyList, and handles the JSON serialization of the property.

public class PropertyListBase<T> : PropertyList<T>
{
    public PropertyListBase()
    {
        _objectSerializer = this._objectSerializerFactory.Service.GetSerializer("application/json");
    }
    private Injected<ObjectSerializerFactory> _objectSerializerFactory;
 
    private IObjectSerializer _objectSerializer;
    protected override T ParseItem(string value)
    {
        return _objectSerializer.Deserialize<T>(value);
    }
 
    public override PropertyData ParseToObject(string value)
    {
        ParseToSelf(value);
        return this;
    }
}

 

CollectionEditorDescriptor
Finally, add an editor descriptor of type CollectionEditorDescriptor<T> to the property. This will give you a handy editor for the edit view, as shown in the images.

[EditorDescriptor(EditorDescriptorType = typeof (CollectionEditorDescriptor<Contact>))]
public virtual IList<Contact> Contacts { get; set; }

 

add

Nov 02, 2015

Comments

Please login to comment.
Latest blogs
Optimizely Configured Commerce and Spire CMS - Figuring out Handlers

I recently entered the world of Optimizely Configured Commerce and Spire CMS. Intriguing, interesting and challenging at the same time, especially...

Ritu Madan | Mar 12, 2025

Another console app for calling the Optimizely CMS REST API

Introducing a Spectre.Console.Cli app for exploring an Optimizely SaaS CMS instance and to source code control definitions.

Johan Kronberg | Mar 11, 2025 |

Extending UrlResolver to Generate Lowercase Links in Optimizely CMS 12

When working with Optimizely CMS 12, URL consistency is crucial for SEO and usability. By default, Optimizely does not enforce lowercase URLs, whic...

Santiago Morla | Mar 7, 2025 |

Optimizing Experiences with Optimizely: Custom Audience Criteria for Mobile Visitors

In today’s mobile-first world, delivering personalized experiences to visitors using mobile devices is crucial for maximizing engagement and...

Nenad Nicevski | Mar 5, 2025 |

Unable to view Optimizely Forms submissions when some values are too long

I discovered a form where the form submissions could not be viewed in the Optimizely UI, only downloaded. Learn how to fix the issue.

Tomas Hensrud Gulla | Mar 4, 2025 |

CMS 12 DXP Migrations - Time Zones

When it comes to migrating a project from CMS 11 and .NET Framework on the DXP to CMS 12 and .NET Core one thing you need to be aware of is the...

Scott Reed | Mar 4, 2025