List of pages that uses visitor groups

Vote:
 

Hi, I'm trying to list all pages that uses visitor groups, similar to this report: https://world.episerver.com/blogs/Paul-Smith/Dates1/2011/8/Visitor-Group-Usage-Report/

I tried this code which works with one page.

var page = contentRepo.Get<IContent>(ContentReference.Parse("9"));

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));

But I can't seem to find a way to get all pages and make it work.

I'm thinking something like this, if possible. 

foreach (var page in pages)
{
       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)
       {
             usedOnThesePages.Add(page);

       }
}

Any suggestions?

#217193
Edited, Feb 19, 2020 8:47
Vote:
 

Hi,

I think you've got a few options here and the correct one will depend on how your site is constructed and at what point you're planning to use the data.

I think the simplest route would be to use IContentLoader.GetDescendents(contenRef) to get a list of contentreferences of all content below the ContentReference you pass in. You can then loop through these and, for each one, use IContentLoader.Get<PageData>(ContentRef) to get the page. In this scenario I'd avoid getting all of the PageDatas in one go by doing something like IContentLoader.GetDescendents(contenRef).Select(x => contentLoader.Get<PageData>(ContentRef)) as you don't want to overload the server memory. Something like this:

var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
var personalisedPages = new List<ContentReference>();
foreach (var pageRef in contentLoader.GetDescendents(SiteDefinition.Current.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);
    }
}

If this needs to be queried in response to a user request, you may be better adding the result of isPersonalised to a property indexed in Find then querying the data from Find instead of using IContentLoader.

#217202
Feb 19, 2020 10:30
Vote:
 

Just what I was looking for !

Thanks!

#217214
Feb 19, 2020 12:25
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.