When a block or media item is published get all pages using the block or media item

 

Hi,

I need to get all the pages that use a block or media item when that block or media item is published. I can hook into the publish event ok and I can get pages using

var repository = ServiceLocator.Current.GetInstance<IContentRepository>();
var references = repository.GetReferencesToContent(e.ContentLink, true).ToList();

However if a block is nested within a block the above will only get the parent block and not the page where the parent block is used. Similarly if a block was nested several times then it would only get the immediate parent.

Has anyone done anything similar in the past to get the ultimate page using the block?

Thanks,

Mark

#201249 Feb 11, 2019 13:00
  • Mattias Olsson
    Member since: 2005
     

    I think you’re stuck with recursive GetReferencesToContent if reference is a block.

    I’m curious about the case here. Are you updating changed date on the page when a block or media file is changed?

    #201281 Feb 12, 2019 8:19
  •  

    Yes, that seems to be the issue, I get parent from child but when I get the references for the parent it gives me the child.

    Not exactly but that would be a useful case. What we need to do is remove a page from a custom cache when anything used by the page is updated, block or media.

    Any suggestions? I looked into getting parent items but for blocks this is the folder where the block 'lives' rather than the page.

    #201293 Feb 12, 2019 14:39
  • Jeroen Stemerdink
    Member since: 2009
     

    I do something similar in my "BlockSearch" package. You can use something like this maybe?

    List<ContentReference> referencingContentLinks = this.ContentSoftLinkRepository.Load(contentLink: contentLink, reversed: true)
                        .Where(
                            link =>
                            link.SoftLinkType == ReferenceType.PageLinkReference
                            && !ContentReference.IsNullOrEmpty(contentLink: link.OwnerContentLink))
                        .Select(link => link.OwnerContentLink)
    .ToList();

    For the complete code have a look at the complete helper on GitHub

    #201295 Feb 12, 2019 15:25