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


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.

Edited, Mar 23, 2018 17:25

You can use the IContentRepository for that 

Check out

Mar 23, 2018 18:00

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))
Mar 24, 2018 15:56

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

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 -
* 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.