A very frequent requirement on projects is to give content editors the ability to pick from a list of 'things'. This list is usually either a drop-down or check-box list. In the majority of situations, the data required to populate a drop-down list or checkbox list is populated from either page types or enum. In EpiWord this data population can be achieved by a thing called a selection factory
What is a Selection Factory?
We have a project manager on our project who learned the phrase" selection factory". His definition is interchangeable with a drop-down picker and for all purposes, this is a good enough definition for the majority of cases. When you start to work with a selection factory, you will get very familiar with the SelectOne and SelectMany attribute. When you decorate one of your page or block properties with a SelectOne or SelectMany attribute, you will also need to provide a data source to populate it.
This data source is the selection factory. A SelectOne will give your content editors a single selection control and a SelectMany will provide a multiple selection option. Each property requires a SelectionFactoryType to be included. This is where you will need to create some custom code to pull back the data you care about. To create a selection factory you'll need to create a class implementing the ISelectionFactory interface.
On your page or block type, you then decorate your property with a SelectOne/SelectMany and add your CurrentSelectionFactory and job done!
And the selection factory code: When you implement ISelectionFactory you'll need to implement:
It's this section of code where you can add your code to fetch whatever data you need.
A Page Type Selection Factory
Now we've gone over the basic example that hard codes return values, let's go over the code you'll probably be using the majority of time when you are working with selection factories... code that actually returns dynamic data. In the example below, we want to return a list of category page types that live under a category root page. In this project, we have the root page defined in the homepage. In the get root page method, we search the homepage to find the reference.
When you're building your site, I would not always recommend using this pattern to store your settings. In this example it makes the code a bit easier to understand but I would recommend reading The Best Ways To Store Site Settings in Episerver for more insight how to architecture your settings in your project.
In the GetSelections() method we call GetCategories(). In here, we first get a reference to the root page where all our category pages are stored. I then iterate through the list and add them in a Category object. In a high traffic project, I would usually use the dynamic data store or cache to save these values for a temporary period, to prevent the constant trips to the database. After that we return the items as ISelectItem and we're done.