Quite often a lot of the content on pages in Optimizely Content cloud (aka Episerver CMS) is structured in blocks placed in content areas. And often even blocks in blocks. Sometimes it's needed to quickly get a list of all the content items on a page - and sometimes you might also be interested in which visitor groups are used. Here is a couple of extension methods to help you with that.
Below are two extension methods that have proven themselfes quite useful to me when working with Optimizely/Episerver solutions.
The first will for a given IContent return all IContent used - including itself.
This means all blocks, media and so on that is in either ContentAreas or Xhtmlstrings in it. Note, you might have other contentreferences - but they are not used here, so you'd have to extend it with logic for that.
An alternative approach is to use the IContentRepository.GetReferencesToContent(...) but that returns quite a lot more than what you often need. Same goes for the SoftLinks repository which has a slightly similar functionality.
The other extension method extends the IVisitorGroupRepository and can help you extract a list of visitor groups used in an IContent - namely in Xhtmlstrings and ContentAreas which are the two places personalization is often applied. Note: personalization can of course also be used for content access control - that is not included in this method.
You can of course combine them - and that can be quite useful. For example if you want to know which visitor groups are used on a page (and it's blocks):
IContent c = _contentRepo.Get<IContent>(currentContent);
var referencedContent = _contentRepo.FetchReferencedContentRecursively(c);
var groups = referencedContent.SelectMany(rc => _vgRepo.ExtractVisitorGroups(rc)).Distinct().ToList();
Some usecases I have recently used them on includes:
- Generate reports with an overview of which visitor groups are used on which pages
- List suggestions for previewing a piece of content with the visitor groups that will take effect.
- Content collection for AI processing for an entire page.
Let me know what you can use them for - and watch out for future blog posts with more details on the usecases above.