How can I find out if a block is being used on any published page?

Ayo
Ayo
Vote:
 

I have an episerver project that has lots of blocks over time some of these blocks are no longer needed, is there a way to see if a created block is being used on any pages in my episerver project?

Potentially want to remove block types that will no longer be used from the code, but I'm not sure if it may break something down the line.

#189764
Edited, Mar 23, 2018 17:25
Vote:
 

You can use the IContentRepository for that 

Check out 

https://world.episerver.com/documentation/Class-library/?documentId=cms/10/72E612B0

#189765
Mar 23, 2018 18:00
Vote:
 

This should give you a list of contents where the block is being referenced.

var contents = new List<ContentData>();
var repository = ServiceLocator.Current.GetInstance<IContentRepository>();
IEnumerable references = repository.GetReferencesToContent(contentLink, false);
foreach (ReferenceInformation reference in references)
{
    ContentReference ownerContentLink = reference.OwnerID;
    CultureInfo ownerLanguage = reference.OwnerLanguage;
    ILanguageSelector selector = new LanguageSelector(ownerLanguage.Name);
    var content = repository.Get<ContentData>(ownerContentLink, selector);

    var contentArea = content["ResponsibleEditors"] as ContentArea;
    if (contentArea != null)
    {
        if (contentArea.ContentFragments.Any(fragment => fragment.ContentLink == contentLink))
            contents.Add(content);
    }
}
#189810
Mar 24, 2018 15:56
Vote:
 

and when you do your homework - please don't use service locator :)

#189813
Mar 24, 2018 20:36
John walsh - Nov 25, 2019 19:05
Can you elaborate on why Service Locator should not be used please?
valdis - Nov 26, 2019 5:35
I should be more precise here - please don't use ServiceLocator "directly". Of course at the end there should be somebody that is responsible for resolving dependencies and instantiating services, but direct usage in your code is anti-pattern. I'll not try to explain, as Mark did it much better - https://blog.ploeh.dk/2010/02/03/ServiceLocatorisanAnti-Pattern/
* 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.