In this tutorial, you will learn about the
IVisitorGroupRepository. If you introduce a level of personalisation to your site visitors within an Episerver CMS-powered website, you will need to use visitor groups. Visitor groups allow you to create user segments easily. If you want to learn how to access these groups in code, read on 🔥🔥🔥
Out-of-the-box, Episerver ships with a number of pre-baked visitor groups. It is also possible to create your own visitor groups yourself in code. This means that on a typical project you will have a number of visitor groups defined within the CMS. When implementing personalisation, a pretty common task is to query the CMS in code about all the available visitor groups, how do we do that?
If you want to query the CMS about visitor groups, you will need to use the IVisitorGroupRepository API. The Visitor Group Repository has a number of useful methods for doing things, like creating, updating and deleting visitor groups:
The code above gets a list of all the visitor groups within the CMS and returns them within a list. This can then be used as the data source for either a drop-down list or checkbox property within the Episerver editor (read this to learn how to do that). On Line15, I access the API using dependency injection and the Epsirver property
Injected<T>. On Line20, the code uses
GetSelections() to return a list of visitor groups to the client. It's that simple. When working with visitor groups,
IVisitorGroupRepository is definitely your friend. Happy Coding 🤘