How to Create a Selection Factory From a Page Type in Episerver

A new feature to Episerver is the Selection Factory.  In this blog post we are going to give a quick and dirty example 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.

[ContentType(
DisplayName = "Category Root Page",
GUID = "58A42E12-C501-4340-A7D9-D8FAA4E3CA74",
Description = "Blog Home Page",
GroupName = "Category Root Page")]
public class CategoryRootPage : PageData
{
}

Next we will define the CategoryPage that will get used as the selection factory’s data items.

[ContentType(
DisplayName = "Category Page",
GUID = "68C1BCCC-34E9-42CE-98C5-05BE3A0EBAF0",
Description = "Category Name",
GroupName = "Blog")]
public class CategoryPage : PageData
{
[Display(
Name = "Category Name",
Description = "Category Name",
GroupName = SystemTabNames.Content,
Order = 100)]
public virtual string CategoryName { get; set; }
}

Now we have our page types defined, we can create our data:

SelectionFactory_1

The next thing we do is define the selection factory:

public class CategoriesFactory : ISelectionFactory
{
public Injected<IContentRepository> contentRepository;
public virtual IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
{
return GetCategories().Select(x => (new SelectItem { Text = x.CategoryName, Value = x.ContentLink.ID.ToString() }));
}
protected IEnumerable<CategoryPage> GetCategories()
{
var categoryRoot = GetCategoryRootPage();
var pages =  contentRepository.Service.GetChildren<CategoryPage>(categoryRoot.ContentLink);
return pages;
}
private CategoryRootPage GetCategoryRootPage()
{
var rootPage = contentRepository.Service.Get<CategoryRootPage>(new ContentReference(40));
if (rootPage == null)
throw new InvalidOperationException();
return rootPage;
}
}

To get the selection factory up and running, we create a class that implements from ISelectionFactory and implements :

IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)

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 dropdown 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.

Th last thing we need to do is decorate a property on a page or block to render the selection factory.

[Display(Name = "Blog Category",
GroupName = SystemTabNames.Content,
Order = 100)]
[SelectOne(SelectionFactoryType = typeof(CategoriesFactory))]
public virtual string BlogCategory { get; set; }

When you run EServer, you should see a Dropdown populated with the category data:

SelectionFactory_2

Conclusion

In today’s guide we’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.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

1 reply

Trackbacks & Pingbacks

  1. […] You may notice I’m using a [SelectOne] for the blog category. I won’t cover the code here (it’s in the associated project download) but if you want to learn more about selection factories I recommend reading How to Create a Selection Factory From a Page Type in Episerver […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *