0

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.

1 Answers1

2

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.

Johan Petersson
  • 972
  • 4
  • 13
  • Thanks for the reponse Johan.This will work but only to an extend where I have a block directly placed on a page.If there is a block on another block which is on a page,this will return empty.For instance ,I have a download block which is placed on a two column block content area .This two colum block in turn is placed on a page content area;then this Icontentsoftlinkrepository will not work in this case.I hope I am making sense here. – Farhin Shaikh Apr 13 '22 at 22:21
  • 1
    Make the code traverse until you reach a page? – Johan Petersson Apr 14 '22 at 23:04
  • I have written a recursive function (added code on the question)for this but it works only on a single level .Would appreciate ur inputs if any – Farhin Shaikh Apr 15 '22 at 09:19
  • 1
    But parent is not what you want to evaluate. I meant that you should use IContentSoftLinkRepository recursively. Parent is the parent in the content tree, not necessarily the the content that has the block. – Johan Petersson Apr 20 '22 at 09:24
  • Thanks Johan .Using IContentSoftLinkRepository recursively worked. I edited the question to include the updated working code. – Farhin Shaikh Apr 29 '22 at 13:35