How to check how many times a block is used ?

Vote:
 

Hi,

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

#188500
Feb 26, 2018 11:46
Vote:
 
        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.

#188503
Feb 26, 2018 12:40
Vote:
 

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. 

#188505
Feb 26, 2018 13:33
Vote:
 

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

#188563
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.