I have previously written about Visitor Groups and how you can employ them to provide tailored, personalised content for your websites visitors. In a lot of scenarios, the default criteria won't be enough to meet all your business requirements. When this happens it's time to bust out visual studio and build your own ones.

In today's article I am going to go over all the code you need. 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.

Even though Episerver provides a lot of visitor group criteria out of the box, on most projects is it is extremely likely that you will need to create your own custom one.  In today's guide, I'm going to cover how you can create one.

The Settings File

The first step is to create the CriterionModelBase

The basics of the code are quite straight-forward, inherit from CriterionModelBase, implement the Copy() method and any properties that you want to be displayed to the content editor. One thing you should notice is the fact that the IValidateCriterionModel interface is optional. When you create your own custom criterion you do not need to implement it. This interface is optional and you do not have to implement it.

When you add IValidateCriterionModel to your settings class you are adding editor level validation that will be called when any visitor group that implements your criterion is saved. The IValidateCriterionModel supplies the Validate method that you can see above. You can add any custom validation code in here. This method will be called when a content editor saves a visitor group that contains the criterion.

The Criterion

After you add the following snippets, when you run your site, go to the visitor groups tab. Create a new visitor group and in the criteria section you should see your criteria. episerver_custom_criterion