Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Loading...
Area: Optimizely CMS
ARCHIVED This content is retired and no longer maintained. See the latest version here.

Recommended reading 

Single or multiple list options

This document describes how to set up single/multiple selection from a list of predefined values using the attributes located in the EPiServer.Shell.ObjectEditing namespace in the EPiServer.UI assembly: SelectOne and SelectMany. These can be defined on a property and requires a reference to a class implementing the ISelectionFactory interface:

[ContentType]
public class SamplePage : PageData
{
    [SelectOne(SelectionFactoryType=typeof(LanguageSelectionFactory))]
    public virtual string SingleLanguage { get; set; }
 
    [SelectMany(SelectionFactoryType = typeof(LanguageSelectionFactory))]
    public virtual string MultipleLanguage { get; set; }
}
 
public class LanguageSelectionFactory : ISelectionFactory
{
    public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
    {
        return new ISelectItem[] { new SelectItem() { Text = "English", Value = "EN" }, new SelectItem() { Text = "Swahili", Value = "SW" }, new SelectItem() { Text = "French Polonesia", Value = "PF" }
}; } }
 
The result looks something like this:

Creating your own attributes

Since you want to follow the DRY principle and avoid adding the reference to the selection factory in a lot of attributes it might be good creating your own attribute if you will use them in several places. This can be done by inheriting from the EPiServer attributes and just overriding the SelectionFactoryType property:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class LanguageSelectionAttribute : SelectOneAttribute
{
    public override Type SelectionFactoryType
    {
        get
        {
            return typeof(LanguageSelectionFactory);
        }
        set
        {
            base.SelectionFactoryType = value;
        }
    }
}
Do you find this information helpful? Please log in to provide feedback.

Last updated: Sep 19, 2014

Recommended reading