In this tutorial, you will learn how to create a dropdown list within the Episerver editor whose data source is powered by a CMS page. While content modelling, if you want content editors to be able to add data onto a CMS page using either a checkbox or dropdown list you will need to learn about selection factories. Any dropdown or checkbox list will need some form of data source. The selection factory is the Episerver thing you will need to build in order ot inject data correctly. To use a selection factory, you will need to decorate a property with either the SelectOne
or SelectMany
attribute (found in the Episerver.Shell.ObjectEditing
namespace). First, we need to define two-page types. A container page that acts as the parent bucket and some child pages that will be used as the data for the property. The parent page-type code could look like this:
The page-type definition to create the data options look like this:
With these page types added within the CMS, a content editor could go into the editor and create the pages that will be used as the data source for the property. The structure of which would look something like this:
With the page-types to add the data define. You need to create the selection factory to inject the data into the property:
To get the selection factory up and running, create a new class that implements from ISelectionFactory
:
To return data to the editor you will need to add some code within GetSelections()
. The code should hopefully be fairly intuitive. Get a reference to the container page type and return the children as the data items. In the code I get the container page using, GetCategoryRootPage()
. For this example, I'm hard coding the content ID of the container page within the editor. You may want to use app settings or a settings page to store this value in production for easier management (read this for more information) 😉
The next step is done within GetCategories()
. This method retrieves all the child pages underneath the container page. In GetSelections()
, I'm building up an enumerable list of type SelectItems
using the CategoryName
as Name
and the label and ID
as the dropdown value identifier
The last step is to decorate the property ontp a page or block type:
If you viewed the dropdown in the editor, it should look something like this:
You know the basics on how to get a selection factory up and running in Episerver. First, define the data. In this example the page types. Next, create a selection factory by implementing from the ISelectionFactory
interface to return the data within the editor. We decorated BlogCategory
with the [SelectOne]
attribute and pointed to the SelectionFactoryType
to use the custom selection factory we created. If you want a user to select multiple items then we can change the [SelectOne]
to a [SelectMany]
attribute. Happy Coding 🤘