How To Hook Into Episerver Visitor Group Save and Delete Events

When you create a Visitor Group in Episerver, there may be occasions when you need to perform additional logic when a visitor group is deleted or saved. Maybe you only want a single type of visitor group to be created, or, you only want a visitor group to be deleted within a certain time frame. In today’s tutorial, I’m going to cover the skeleton code that will allow you to hook into visitor group events, so you can add your own custom logic.

If you are new to Visitor Groups and need an introduction, then I suggest that you read What Is An Episerver Visitor Group ?.

Adding Validation When Saving A Visitor Group

If you want to hook into the visitor group SavedVisitorGroup, or, SavingVisitorGroup events. Below demonstrates an initialization module that will allow you to write custom logic for your events.

[InitializableModule]
public class VisitorGroupEventsInitialization : IInitializableModule
{
public void Initialize(InitializationEngine context)
{
VisitorGroupEvents.Instance.SavedVisitorGroup += SaveVisitorGroup;
}
private void SaveVisitorGroup(object sender, VisitorGroupEventArgs e)
{
// Validate Visitor Group;
if (e.VisitorGroup.Name == "VisitorGroup")
{ 
e.CancalAction = true;
}
}
public void Uninitialize(InitializationEngine context)
{
}
public void Preload(string[] parameters)
{
}
}
}

In the code above, we use the ‘VisitorGroupEvents’ API to hook into the visitor groups events. Note, this is a static class. Simply register an event from the options avilable and create a matching method. In here, you are now free to write any custom logic you want.

Adding Validation When Deleting A Visitor Group

Hooking into the delete event is very similiar to hooking into the saving events. This time we can use either
DeletingVisitorGroup, or, DeletedVisitorGroup.

[InitializableModule]
public class DataInitialization : IInitializableModule
{
public void Initialize(InitializationEngine context)
{
VisitorGroupEvents.Instance.DeletingVisitorGroup += DeletingVisitorGroup;
}
private void DeletingVisitorGroup(object sender, VisitorGroupEventArgs e)
{
// Do logic here
}
public void Uninitialize(InitializationEngine context)
{
}
public void Preload(string[] parameters)
{
}
}

The API documentation for this can be found here, VisitorGroupEvents Class

Conclusion

In today’s post, we’ve hooked into the visitor groups events to add bespoke logic that dictates if a visitor group can be created, or, deleted. For example, you may want to create a system that prevents any visitor group from being deleted.

In order to hook into visitor group events, we can use the Episerver VisitorGroupEvents API. The API comes with four events, DeletedVisitorGroup, DeletingVisitorGroup, SavedVisitorGroup and SavingVisitorGroup. Enjoy!

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 *