episerveroptimizely

Get Page url on block types


I am working on generating a report in episerver in the form of a scheduled job.This report is basically used to get all the contents(page types,blocks,media) within Episerver.

There are some good nuget packages for content usages but for some reason& to have more control & plans for tweeking & extending it further,i am creating a custom one rather than using the available 3rd party packages.

The scheduled job is kind of similar to https://www.codeart.dk/blog/2018/12/content-report-generator/ . The article helped me a lot to get my report working with some modifications as per my requirement.

The one thing that I am struggling here is to get the URL of where the block is. Now this is a point of debate here. I am aware that the blocks being shared in nature can be used anywhere in a site but the point is they are still used in pages or as a matter of fact in other blocks which is turn is used on a page.What i am trying to say here is,they directly or indirectly are part of a page.So is there a way to get the page url of a block irrespective of how many pages they are in. Every forum I have looked at ,there's always page url of Pagedata or mediadata & nothing on blockdata.Even 3rd party nuget packages that i have looked for does not have page url for block types.

I do understand that there is nothing out of the box here.Is there a way to achieve this ie get the page url of a specific block type which can be a list of page urls if the block is used in multiple pages.

Recursive function to reach the page:

 private string GetPublicUrl(IContentRepository contentRepository, IContentSoftLinkRepository contentSoftLinkRepository, ContentReference contentReference)
        {
            var publicUrl = string.Empty;
            var content = contentRepository.Get<IContent>(contentReference);
            var referencingContentLinks = contentSoftLinkRepository.Load(content.ContentLink, true)
                .Where(link => link.SoftLinkType == ReferenceType.PageLinkReference && !ContentReference.IsNullOrEmpty(link.OwnerContentLink))
                .Select(link => link.OwnerContentLink);

            foreach (var referencingContentLink in referencingContentLinks)
            {
                publicUrl = UrlResolver.Current.GetUrl(referencingContentLink.GetPublicUrl()) ?? GetPublicUrl(contentRepository, contentSoftLinkRepository, referencingContentLink);

            }

            return publicUrl;
        }

I have written this recursive function to reach the page ,but this works only when there is a single level. For instance A 2Col block on a page. If I have a block say Download Block which is on a 2Col block which in turn is on a page ,then in this case the url is empty.

Any input is appreciated.


Solution

  • With IContentSoftLinkRepository you can find where the blocks are used. You can check whether the SoftLink points to a page with SoftLink.SoftLinkType == PageLinkReference and then use IUrlResolver to get the page's URL.