How To Create A Visitor Group Selection Factory

I’ve been working a lot with promotions and discounts recently.  A big part of the process is being able to restrict/customise different discounts to different users on the site. In Episerver, personalising content for users is done via a visitor group.  If you want more information about visitor groups, or, selection factories, then I would recommend reading What Is A Episerver Visitor Group ? and How to Create a Selection Factory From a Page Type in Episerver.

In order to be able to do anything useful with a visitor group, you will need to use the IVisitorGroupRepository API. The Visitor Group Repository has a number of useful methods for doing things, like creating, updating and deleting visitor groups.

using System.Collections.Generic;
using System.Linq;
using EPiServer.Personalization.VisitorGroups;
using EPiServer.Shell.ObjectEditing;
using EPiServer.ServiceLocation;
using ISelectionFactory = EPiServer.Shell.ObjectEditing.ISelectionFactory;
namespace GMGShop.Model.SelectionFactory
{
public class VisitorGroupSelectionFactory : ISelectionFactory
{
internal Injected<IVisitorGroupRepository>  VisitorGroupRepository;
public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
{
return
VisitorGroupRepository.Service.List()
.OrderBy(v => v.Name)
.Select(v => new SelectItem { Text = v.Name, Value = v.Id });
}
}
}

Most of the code above should look semi-familiar to anyone who has created a visitor group before.  Create a class that inherits from ISelectionFactory, implement GetSelections , then use the VisitorGroupRepository to return a list of select items to the client. It’s that simple

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

0 replies

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 *