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.
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.
The API documentation for this can be found here, VisitorGroupEvents Class
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!