Try our conversational search powered by Generative AI!

How to check how many times a block is used ?



As the subject of this topic, I need to check a block is used or never used, and how many times that a block is used on the site.

Has anyone the solution for my issue?

Thanks very much, :D

Feb 26, 2018 11:46
        private readonly IContentTypeRepository _contentTypeRepository;
        private readonly IContentModelUsage _contentUsageLoader;

        private IEnumerable<ContentUsage> GetBlockReferencesByType<T>() where T : BlockData
            var contentType = _contentTypeRepository.Load<T>();
            var usages = _contentUsageLoader.ListContentOfContentType(contentType).ToList();
            return usages;

Beware the performance implications as this isn't a common use case for run-time it doesn't come with built-in performance optimization.

Feb 26, 2018 12:40

This is basically a function for editors, so yes, it's not performance optimized (would be quite difficult as well). So make sure you don't use this on an end-user faced feature. 

Feb 26, 2018 13:33

Oh I wanna find a block is used how many times, not how many instances are there a Block Type has.
But I found the solution for my question.

var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
var timesIsUsed = contentRepository.GetReferencesToContent(block.ContentLink, false);

Thanks for your answer :D

Feb 27, 2018 3:50
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.