How To Create An Episerver Visitor Group Via Code

In this post, I am going to cover how to create/add a visitor group into your solution via code. If you are not quite sure what a visitor group is, or if it’s the right fit for your need, then I would recommend reading What Is A Episerver Visitor Group ?

Episerver provides us several ways to interact with visitor groups; one way is the VisitorGroupHelper found in EPiServer.Personalization.VisitorGroups. This helper will check if the current user is valid for a certain visitor group.

For example:

var visitorGroupName = "VisitorGroupName";
var groupHelper = new VisitorGroupHelper();
var isPrincipalInGroup = groupHelper.IsPrincipalInGroup(
PrincipalInfo.CurrentPrincipal, visitorGroupName);

For this tutorial, we will mainly be focusing on the IVisitorGroupRepository


The visitor group repository provides access to standard functions like, save a visitor group, delete a visitor group, list all visitor groups and load a visitor group by Id.

To create and save a visitor group we need to use the IVisitorGroupRepository and a VisitorGroup object.

var visitorGroupRepository = ServiceLocator.Current.GetInstance<IVisitorGroupRepository>();
var visitorGroup = new VisitorGroup
Name = "Visitor Group Name"

The code above is quite straightforward. Get an instance of IVisitorGroupRepository from the Service locator, create a VisitorGroup and use the save method.


This code is all very well, but, I’m sure you will probably want your visitor group to do something useful. This happens by adding criterions to the visitor group before you save it using the IVisitorGroupCriterionRepository.


In order to add criterions to your visitor group in code, you will need to use the IVisitorGroupCriterionRepository. If you try to just assign one directly by newing up a criterion yourself when you try and save the visitor group you will get an Object Reference exception.

Luckily, the IVisitorGroupCriterionRepository is pretty easy to use:

var visitorGroupCriterionRepository =            ServiceLocator.Current.GetInstance<IVisitorGroupCriterionRepository>();
var criterions = VisitorGroupCriterionRepository.Service.List();
var criterion = criterions.Where(x => x.TypeName.Contains("MyCustomCriterion"));

The code is pretty simple, IVisitorGroupCriterionRepository only exposes one method, the list method that gets all the visitor groups that have been defined in your solution.

You can then use the TypeName property to return the criterion that you care about. Add the criterion to the visitor group is pretty simple:

var visitorGroupCriterionRepository = ServiceLocator.Current.GetInstance<IVisitorGroupCriterionRepository>();
var visitorGroupRepository = ServiceLocator.Current.GetInstance<IVisitorGroupRepository>();
var criterions = visitorGroupCriterionRepository.List();
var criterion = criterions.Where(x => x.TypeName.Contains("MyCustomCriterionType"));
var visitorGroup = new VisitorGroup
Name = "Visitor Group Name"
if (criterion != null && criterion.Any())
visitorGroup.Criteria = criterion.ToList();


In today’s guide, we have used the IVisitorGroupRepository and IVisitorGroupCriterionRepository repositories. We can use IVisitorGroupRepository to get, save and update visitor groups. We can use the IVisitorGroupCriterionRepository to get all the criterions defined within the solution in order to attach them to a visitor group.

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

2 replies

Trackbacks & Pingbacks

  1. […] In today’s guide, we are going to go over everything you will need to be able to check if a current request matches your visitor group.  In order to do this we will be using the IVisitorGroupRepository. If you haven’t come across this API yet, then I would recommend reading this first, How To Create An EpiServer Visitor Group Via Code […]

  2. […] If you need some extra information about visitor groups I recommend reading What Is A EpiServer Visitor Group ? and How To Create An EpiServer Visitor Group Via Code […]

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 *