Reputation: 49
I am trying to create custom report for Blocks list and usages int pages. So I need to get all list of block types available and the usages/links in pages.
I have tried the below code but this requires Block as input to fetch usages.
var repository = ServiceLocator.Current.GetInstance<IContentRepository>();
var contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
var contentModelUsage = ServiceLocator.Current.GetInstance<IContentModelUsage>();
var myblockType = contentTypeRepository.Load<InputBlock>();
List<ContentReference> myblockTypeReferences = contentModelUsage.ListContentOfContentType(myblockType).Select(x => x.ContentLink.ToReferenceWithoutVersion()).Distinct().ToList();
InputBlock blockType;
foreach (ContentReference cref in myblockTypeReferences)
{
repository.TryGet<InputBlock>(cref, out blockType);
model.lstBlocks.Add(blockType);
}
I expect to not provide input block and get all list of block types available in Episerver and usages in pages
Upvotes: 0
Views: 3436
Reputation: 868
You would need to loop through all the content types and then loop through usages of all those content types. I used below function in Epi 11
public IList<IContent> GetUnusedBlocks()
{
var contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
var contentModelUsage = ServiceLocator.Current.GetInstance<IContentModelUsage>();
var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
var linkRepository = ServiceLocator.Current.GetInstance<IContentSoftLinkRepository>();
var unusedBlocks = new List<IContent>();
// Loop through all content types
foreach (var contentType in contentTypeRepository.List().Where(content => content.Name.ToLower().Contains("block")))
{
// Find usages of the content type in question
IList<ContentUsage> contentUsages = contentModelUsage.ListContentOfContentType(contentType);
// Get usages including versions
IList<IContent> blocks = contentUsages.Select(contentUsage =>
contentUsage.ContentLink.ToReferenceWithoutVersion())
.Distinct()
.Select(contentReference => contentRepository.Get<IContent>(contentReference))
.ToList();
// Find blocks that are being referenced
foreach (IContent block in blocks)
{
var referencingContentLinks = linkRepository.Load(block.ContentLink, true)
.Where(link =>
link.SoftLinkType == ReferenceType.PageLinkReference &&
!ContentReference.IsNullOrEmpty(link.OwnerContentLink))
.Select(link => link.OwnerContentLink);
if (!referencingContentLinks.Any())
{
unusedBlocks.Add(block);
}
}
}
return unusedBlocks;
}
Reference: EpiServer - How can I find out if a block is being used on any published page?
Upvotes: 1
Reputation: 26
If you need to get the Content Types, you can IContentRespository List() method.
To get actual instances of content, you can use IContentModelUsage ListContentOfContentType method. That will grab all the instances.
var usages = contentModelUsage.ListContentOfContentType(contentType);
There is also a free app in Episerver's new App Marketplace that I think does something similar to the functionality you are seeking if you want to check it out.
edit: I see you tagged this with Episerver 8, and the app is only with 11 and above, so that may not work for you.
Upvotes: 0