Filter pages on specific visitor group

Vote:
 

Hi,

I have a list of UsedVisitorGroups, I used the code from david-tecs Visitor Group Usage Viewer  https://www.david-tec.com/2015/05/visitor-group-usage-viewer-for-episerver-8/

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)
{
personalisedPages.Add(pageRef);
}
}

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 https://world.episerver.com/globalassets/globals/xmlrpc/21272/2011/08/18/image_2.png

Suggestions?

Any help very much appreciated!

#217296
Feb 20, 2020 14:31
Vote:
 

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.

#217302
Feb 20, 2020 16:17
Vote:
 

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:
https://github.com/nicolaayan/EpiserverCmsAudit/blob/master/N1990.Episerver.Cms.Audit/Business/VisitorGroupAudit.cs 

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[groupName].Add(pageRef);
        }
        else
        {
            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.

#217315
Feb 20, 2020 18:18
Vote:
 

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!

#217346
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.