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.
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.
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!