A while ago I saw a forum question about how to Enable/Disable SelectItem in SelectionFactory. I thought that it could be interesting to describe how to implement it.

Standard checkbox list

Let’s start from the basic SelectionFactory usage. To prepare a property rendered as checkbox list we could use selection factory and SelectMany attribute. First we need to create a class that implements ISelectionFactory. The class has only one method that returns list of ISelectItem. In this code sample we will return the list of continents.

Then we need to mark the property to use CheckBoxListEditor as editing widget and ContinentSelectionFactory as data source. One of the easiest ways to achieve this is to annotate the property with SelectMany attribute. Based on the SelectionFactoryType it will create a desired editor in Edit Mode.

With those few lines of code we have prepared an editable list of checkboxes.

extended checkbox list - standard view

Extended checkbox list

Now we would like to extended this property. It should display a list of all elements defined in the data source. Some of them should be disabled in Edit Mode. Let’s not focus on enable/disable condition, but on the way how to prepare widget with readonly elements. Of course we could create everything from scratch, but let’s try to reuse EPiServer framework code as much as possible.

Backend development

Selection factory contains GetSelections method that returns IEnumerable<ISelectItem>. Thanks to C# support for covariance we could return any collection whose elements implement ISelectItem. The example above use SelectItem class, but now we need to pass Enabled property as well. We could create a new class that inherits from SelectItem and add a new field to it.

Now the selection factory returns IEnumerable of ExtendedSelectItem elements. The Enabled property is set for each element. In this example “Australia” and “South America” should not be selectable.

Should we do something more in the backend code? Let’s check how the data are serialized on the client.

I set the debugger breakpoint in CheckBoxListEditor buildRendering method. There is property selections used by the widget to prepare the list.

extended checkbox list - debugger

The enabled property is already on the list! There is nothing more to implement.

The Dojo widget

As we saw while debugging, our checkbox list use “epi-cms/contentediting/editors/CheckBoxListEditor” Dojo widget. In the buildRendering method there is a loop over selection property. The _addCheckBoxForItem method creates a checkbox widget and add this checkbox to checkboxes collection. In this example we could try to override buildRendering method, use checkboxes list and override them.

We need to prepare custom editor that inherits from CheckBoxListEditor and override buildRendering method.

We call the base method with this.inherited(arguments); and then execute our custom code. The _setCheckboxEnabled method sets the checkbox state to ReadOnly when enabled property is false. This will make the checkbox not selectable.

The last thing to implement is to combine backend and frontent code and use Dojo editor and the selection factory together. We cannot use the SelectMany attribute anymore, because there is no way to pass widget class name. We could prepare a separate EditorDescriptor, but there is a simpler way. We will use ClientEditor attribute which allows us to set both client editing class and selection factory.

The final result is presented below:

extended checkbox list - ReadOnly state

We had to write less than 100 lines of code to get interesting results and change the existing functionality. We now have a powerful mechanism of extending editor based on customer needs.

This shows how flexible the EPiServer properties framework is!

Full source code is available on Gists.