How To Create A Custom Visitor Group Criteria In Episerver 7

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

public class CustomCriterionSettings : CriterionModelBase, IValidateCriterionModel
{
public string MyCustomProperty{ get; set; }
public override ICriterionModel Copy()
{
return ShallowCopy();
}
public CriterionValidationResult Validate(VisitorGroup currentGroup)
{
// Do some editor level validation here!!
var success = true;
var successText = "Validated";
return new CriterionValidationResult(success , successText );
}
}

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.

Important 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

[VisitorGroupCriterion(
Category = "Custom Category",
DisplayName = "Custom Criterion",
Description = "A descirption"
)]
public class CustomCriterion : CriterionBase<CustomCriterionSettings>
{
public override bool IsMatch(IPrincipal principal, HttpContextBase httpContext)
{
return true;
}
}

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

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 *