Implementing Custom Checkbox List Property in EPiServer CMS 7
In one of my recent projects, I’ve had to implement a property that displays a list of checkboxes so the administrator could select an item to enable from the list of available options. I googled around for a bit and came across this article by Linus Ekström.
This is my implementation.
[EditorDescriptorRegistration(TargetType = typeof(string), UIHint = "TransportTypes")]
public class TransportTypeEditorDescriptor : EditorDescriptor
{
public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes)
{
SelectionFactoryType = typeof(TransportTypesFactory);
ClientEditingClass = "epi-cms.contentediting.editors.CheckBoxListEditor";
base.ModifyMetadata(metadata, attributes);
}
}
public class TransportTypesFactory : ISelectionFactory
{
public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
{
var languages = new List<SelectItem>
{
new SelectItem { Value = "train", Text = "Train" },
new SelectItem { Value = "bus", Text = "Bus" },
new SelectItem { Value = "ferry", Text = "Ferry" }
};
return languages;
}
}
You can then implement this in your class like so:
[Display(GroupName = SystemTabNames.Content, Order = 20)]
[UIHint("TransportTypes")]
public virtual string TransportTypes { get; set; }
And you’ll end up with something like this:
Note to CMS 7.1 developers!
As pointed out by Linus in this forum post, you’ll need to replace this line:
ClientEditingClass = "epi-cms.contentediting.editors.CheckBoxListEditor";
with this:
ClientEditingClass = "epi.cms.contentediting.editors.CheckBoxListEditor";
This doesn’t affect CMS 7.2, however. But it obviously begs the question, WHY?