Search code examples
content-management-systemblockepiserver-7

Retrieve pages from block ContentReference


Is it possible to retrieve pages that uses a shared block instance in EPIserver 7.


Solution

  • You can use ContentSoftLinkRepository to get references to/from the content item. Let's say blockLink is the content reference of your block.

    // resolving the repository. It can also be injected as a property or in your constructor.
    var linkRepository = ServiceLocator.Current.GetInstance<ContentSoftLinkRepository>();
    
    // loading soft links for your block
    var referencingContentLinks = linkRepository.Load(blockLink, true).Where(link =>
                        link.SoftLinkType == ReferenceType.PageLinkReference &&
                        !ContentReference.IsNullOrEmpty(link.OwnerContentLink))
                    .Select(link => link.OwnerContentLink)
                    .ToList();
    

    Now you have the list of content links of pages/blocks/... that uses/references your block.