Loading...

Last updated: Feb 04 2019

Area: Episerver CMS Applies to versions: 9.0 and higher

Generic PropertyList (PropertyList<T>)

PropertyList is a property that allows you to define an editable list of objects. Content model can implement a property of type IList<T> where T is a class with property definitions.

public virtual IList<CustomItem> List{ get; set; }

Example - list of locations

This example shows how to implement a list of locations.

Generic PropertyList.png

PropertyList allows the editor to edit items using a dialog window.

Editing Generic PropertyList.png

The list contains items with string properties:

  • address list 1
  • address line 2
  • city
  • country.

First we have to define Location model class. It will represent the property item.

using System.ComponentModel.DataAnnotations;

namespace Site.Models.Pages
{
    public class Location
    {
        [Display(Name = "Address line 1")]
        public virtual string AddressLine1 { get; set; }

        [Display(Name = "Address line 2")]
        public virtual string AddressLine2 { get; set; }

        public virtual string City { get; set; }

        public virtual string Country { get; set; }
    }
}

The next step is to register the property definition. It is done using two classes; LocationsProperty and a base class PropertyListBase. Code can be implemented using single class, but having this class inheritance allows to implement other list types in an easy way.

The base class is responsible for JSON serialization:

using EPiServer.Core;
using EPiServer.Framework.Serialization;
using EPiServer.Framework.Serialization.Internal;
using EPiServer.ServiceLocation;

namespace Site.Models.Pages
{
    public class PropertyListBase<T> : PropertyList<T>
    {
        protected Injected<ObjectSerializerFactory> ObjectSerializerFactory { get; set; }
        private readonly IObjectSerializer _objectSerializer;

        public PropertyListBase()
        {
            _objectSerializer = ObjectSerializerFactory.Service.GetSerializer(KnownContentTypes.Json);
        }

        protected override T ParseItem(string value)
        {
            return _objectSerializer.Deserialize<T>(value);
        }
    }
}

The specific class  LocationsProperty sets the generic type to Location item class:

using EPiServer.PlugIn;

namespace Site.Models.Pages
{
    [PropertyDefinitionTypePlugIn]
    public class LocationsProperty : PropertyListBase<Location> { }
}

Finally we can add list of locations on the page:

using System.ComponentModel.DataAnnotations;
using EPiServer.Core;
using EPiServer.DataAnnotations;
using EPiServer.Cms.Shell.UI.ObjectEditing.EditorDescriptors;
using EPiServer.Shell.ObjectEditing;
using System.Collections.Generic;

namespace Site.Models.Pages
{
    /// <summary>
    /// Page with PropertyListDefinition
    /// </summary>
    [ContentType(GUID = "2CDDC73C-83AC-4F35-BA9D-50F285723A96")]
    public class TestPage : PageData
    {
        [Display(Name = "List of locations")]
        [EditorDescriptor(EditorDescriptorType = typeof(CollectionEditorDescriptor<Location>))]
        public virtual IList<Location> Locations { get; set; }
    }
}

Localizing item properties

List item properties can be translated in a similar way as Content properties translation. Properties can be translated using DisplayAttribute and language files. More information about localized texts can be found here.


Do you have feedback on this documentation? Send an email to documentation@episerver.com. For development-related questions and discussions, refer to our Forums on https://world.episerver.com/forum/