Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tor Langlo 159 posts 419 karma points
    1 week ago
    Tor Langlo
    0

    How to get the URL from an IContent instance

    How do I get (or build) the Url from an IContent instance? In Umbraco 7 the following code worked:

    string url = UmbracoContext.Current.UrlProvider.GetUrl(content.Id);
    

    but in Umbraco 10 the IUrlProvider interface requires an IPublishedContent instance. Interestingly enough, the DefaultUrlProvider's implementation only uses the IPublishedContent.Id property so an overload which accepted just the content Id would work fine (but alas doesn't exist anymore. why?).

    Note, the assumption here is that the IContent instance is not published (although it might be).

    One work-around would be to create an "empty" IPublishedContent instance with just the Id property assigned (how do I do that, is there a suitable/lightweight class implementing IPublishedContent somewhere in the framework?).

    Another work-around would be to "fake" the Url by building one using IUrlSegmentProvider.GetUrlSegment() for each of the IContent's parents. Is there a helper method somewhere already doing this?

  • Roy Berris 53 posts 411 karma points
    1 week ago
    Roy Berris
    0

    I'm not sure if this is true but: I believe you should only be able to generate URL's for a published content. When the document is not published, it is not able to route it. They might've removed the id overload because this violates some rules, or causes some problems that we are not aware of.

    Maybe go back one step, why are you trying to get URL's for nodes that are not published?

  • Tor Langlo 159 posts 419 karma points
    1 week ago
    Tor Langlo
    0

    When reading this, keep in mind we are in the middle of the process of migrating an Umbraco 7 multi-national web site to Umbraco 10. We have code which iterates the content tree for various reasons. This code can’t always make assumptions about whether a content node has been published nor not.

    Reasons for iterating and processing the content tree:

    1. Export of content tree to spreadsheet format (CSV) for the following reasons: a) In general, for various auditing purposes. b) Pending content – compares the content tree on the Dev site to the Live site to give the user an overview of which nodes (pages) have content pending for transfer/publishing, and to see if any content has been directly modified on the Live site (hopefully not). c) Product pages – we have the need to do special processing on the product hierarchy. Each product has its own content node. An example is importing data from a back end product database. d) SEO – audit pages for search engine optimization
    2. Automatic generation of a Google search compatible sitemap. This includes using Umbraco’s relation service to map related languages (cross references between the pages for our supported language sub-sites)
    3. Content maintentance and bug fixes: a) Sometimes content gets messed up or is incorrectly updated. We then need to make one-time tools which will iterate content and fix the data. b) When we kicked-off a new language/country specific sub-site we started the process by copying the content tree for one of the existing sub-sites. After doing this we needed to do various post-processing to clean up the new sub-site. For instance many pages/properties referenced other pages in the old site and needed to be updated to reference the corresponding pages of the new sub-site. This job can was automated with code.

    We also have code which iterates the media tree for various reasons:

    1. Audit media node names, e.g. compare node name to media file name, and ensure names are according to our naming policies
    2. Try to find out where media nodes are used
    3. Review which image types are used, image compression, efficiency, SEO, etc.
  • Tor Langlo 159 posts 419 karma points
    1 week ago
    Tor Langlo
    0

    Here's some rough code for reimplementing what was (I think) already in Umbraco 7. This is untested and also needs to be cleaned up:

    public static string GetUnpublishedUrl(this IContent content, IContentService contentService,
            IUrlSegmentProvider segmentProvider)
        {
            var names = new List<string>();
            while (content != null)
            {
                names.Add(segmentProvider.GetUrlSegment(content));
                content = contentService.GetParent(content);
            }
            names.Reverse();
            return "/" + Koda.Rtl.StringUtils.Join(names, "/")?.ToLower();
        }
    
        public static string GetUrl(this IContent content, IContentService contentService,
            IPublishedContentCache contentCache, IUrlSegmentProvider segmentProvider,
            bool rebuildUrlIfNeeded = false, bool absoluteUrl = false)
        {
            IPublishedContent publishedContent = contentCache.GetById(content.Id);
            string url = absoluteUrl ? publishedContent?.UrlAbsolute() : publishedContent?.Url();
            if (rebuildUrlIfNeeded && (string.IsNullOrEmpty(url) || url == "#"))
            {
                // TODO: Add absoluteUrl parameter to this call
                url = content.GetUnpublishedUrl(contentService, segmentProvider);
            }
            return url;
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft