In this tutorial, you will learn how to write some code that will be triggered whenever a content editor either creates or deletes a visitor group within an Episerver CMS-powered solution. Contained within this page, you will get access to all of the skeleton code that will be required to allow you to hook into Visitor Group 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 want to add some validation to prevent certain data from being added, or, maybe only want a visitor group to be deleted within a certain time frame. If you have a requirement to trigger an event with a visitor group and you are struggling to get it working, this is the tutorial for you 🔥🔥🔥

TIP: If you are new to Visitor Groups and need a more in-depth introduction, I recommend What Is An Episerver Visitor Group?.

In order to hook into visitor group events, you will need to use the VisitorGroupEvents API. This API comes with four events to play with, DeletedVisitorGroup, DeletingVisitorGroup, SavedVisitorGroup and SavingVisitorGroup. Lets us take a look at how we can access them 👀👀

Adding Validation When Saving A Visitor Group

Episerver exposes two save events for visitor groups, SavedVisitorGroup and SavingVisitorGroup. Below demonstrates an initialization module that will allow you to hook into `SaveVisitorGroup:

Within Episerver, an initialization module will be triggered whenever the CMS is first loaded. Using VisitorGroupEvents, you can hook into the visitor group events pipeline to register some custom code. This code will be called whenever a corresponding event occurs within the CMS. Simply, pick the event that you want to hook into and register a method. On line 9, we do that within SaveVisitorGroup. Within this method, you can write your custom code. To access the name of the current visitor group being edited you use e.VisitorGroup. If you want to hook into the SavingVisitorGroup event instead, write the same code but change the event type 💥

Adding Validation When Deleting A Visitor Group

Hooking into the delete events follows the same process as the save events. You can use either DeletingVisitorGroup, or, DeletedVisitorGroup. Deleting will be called before the event and Deleted will be called after the fact. The code to hook into DeletingVisitorGroup is shown below:

It really is as simple as that 🔥🔥🔥

You now know how to hook into Episerver visitor group events to trigger bespoke logic. In order to hook into visitor group events, you will need to make use of the Episerver VisitorGroupEvents API. Happy Coding 🤘d