In this blog post, we are going to give a quick and dirty example of how you can create your own selection factory from a page type in Episerver. To use a selection factory, you will need to decorate a property with either the SelectOne or SelectMany property (in EPiServer.Shell.ObjectEditing). First, we need to define two pages, one container page that acts as the container for the pages the selection factory is going to use.
Next, we will define the CategoryPage that will get used as the selection factory's data items.
Now we have our page types defined, we can create our data:
The next thing we do is define the selection factory:
To get the selection factory up and running, we create a class that implements from ISelectionFactory and implements :
From here it's up to us to write some custom code to return the data we need. If you are still a bit unsure, the process is very similar to how you would populate a drop-down list in MVC, so don't panic. First, we need to get a reference to the parent folder that contains the page types we want to display in the selection factory. This is done in the GetCategoryRootPage(). I'm hard coding the content ID in here for an example.
In the real world, I would recommend reading The Best Way To Manage Global Data Within Episerver
The next part of the GetCategories() method is to retrieve all the child pages underneath the root page. Finally, in GetSelections() I'm building up an enumerable list of SelectItems using the CategoryName as the display name and the page ID as the identifier that will get stored against it. The last thing we need to do is decorate a property on a page or block to render the selection factory.
When you run Episerver, you should see a Dropdown populated with the category data:
In today's guide, I've gone over the basics to get a selection factory up and running in Episerver using existing page types. First, we defined the page types we wanted to work with, we then created a custom election factory by implementing from the ISelectionFactory interface.
Finally, we decorated BlogCategory with the [SelectOne] attribute and pointed 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.