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



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?



Feb 11, 2019 13:00

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?

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.

Feb 12, 2019 14:39

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)
                        link =>
                        link.SoftLinkType == ReferenceType.PageLinkReference
                        && !ContentReference.IsNullOrEmpty(contentLink: link.OwnerContentLink))
                    .Select(link => link.OwnerContentLink)

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

Feb 12, 2019 15:25
* 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.