Filter pages on specific visitor group



I have a list of UsedVisitorGroups, I used the code from david-tecs Visitor Group Usage Viewer

And a list of pages that uses Visitor groups:

var personalisedPages = new List<ContentReference>();

foreach (var pageRef in contentLoader.GetDescendents(ContentReference.StartPage))
var page = contentLoader.Get<PageData>(pageRef);

var isPersonalised = page.Property.Any(p => ((p as IPersonalizedRoles)?.GetRoles().Any() ?? false) ||
(((p as PropertyContentArea)?.Value as ContentArea)?.Items?.Any(x => !string.IsNullOrEmpty(x.ContentGroup)) ?? false));

if (isPersonalised)

I'm trying to match page with visitor group, to be able to filter pages for a specific group.

So the outcome will be something like this


Any help very much appreciated!

Feb 20, 2020 14:31

Hi Runa,

If I understand your requirement correctly I think you could use the CMS Audit tool (available via the Episerver NuGet feed)?

It already supports showing visitor group usage, as detailed in this blog post from Allan.

Feb 20, 2020 16:17

That's some useful info there Jake - I must have missed that blog post. It's worth pointing out that the source code for that tool is available on GitHub so you can see how the visitor group info is gathered here: 

Assuming you don't go down that route, you'd need to slightly change what you're doing in that loop so that, rather than just collectingthe content reference, you're also gathering info on the visitor group with it. In this example, I've stored the data in a Dictionary<string, List<ContentReference>> where the string is the name of the visitor group and the list is the pages using it.

var visitorGroupRepo = ServiceLocator.Current.GetInstance<IVisitorGroupRepository>();
var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
var personalisedPages = new Dictionary<string, List<ContentReference>>();
foreach (var pageRef in contentLoader.GetDescendents(SiteDefinition.Current.StartPage))
    var page = contentLoader.Get<PageData>(pageRef);
    var visitorGroups = page.Property.Where(p => p is IPersonalizedRoles).SelectMany(p => (p as IPersonalizedRoles).GetRoles()).ToList();
    visitorGroups.AddRange(page.Property.Where(p => p is PropertyContentArea && p.Value != null && (p.Value as ContentArea).Items != null).SelectMany(p => (p.Value as ContentArea).Items.SelectMany(x => x.AllowedRoles ?? Enumerable.Empty<string>())));

    foreach (var grp in visitorGroups)
        var groupName = visitorGroupRepo.Load(new Guid(grp)).Name;
        if (personalisedPages.Keys.Contains(grp))
            personalisedPages.Add(groupName, new List<ContentReference> { pageRef });

I should point out that I've not tested this so no guarantees it will work out of the box but hopefully it should give you some inspiration if nothing else.

Feb 20, 2020 18:18

Hi Jake, I missed that blogpost aswell. That solves my problem easier than I thought. Thank you so much!

Thank you again Paul, just what I had in mind!

Feb 21, 2020 9:32
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.