Copied to clipboard

Flag this post as spam?

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


  • Nikola 12 posts 132 karma points
    Jan 30, 2019 @ 10:33
    Nikola
    0

    Get URL of published version from node

    Hello, I have a node which has a published version and is right now unpublished:

    enter image description here

    I would like to get the URL of the published version of this node.

    I managed to get the published version with this line:

    var lastPublishedVersion = ApplicationContext.Current.Services.ContentService.GetPublishedVersion(node);
    

    After that i tried to get the URL with this line:

    var nodeUrl = umbracoHelper.Url(lastPublishedVersion.Id);
    

    This gives my back a "#" which happens (umbraco code documentation)

    If the provider is unable to provide a url, it returns "#".

    How can I get the Url of the published version from a node?

    thanks in advance!

  • Marc Goodson 2157 posts 14431 karma points MVP 9x c-trib
    Jan 30, 2019 @ 12:55
    Marc Goodson
    0

    Hi Nikola

    If you have the id of your content item then

    IPublishedContent currentPublishedVersion = Umbraco.TypedContent(id);
    string publishedUrl = currentPublishedVersion.Url;
    

    This will always return the current published version's url, regardless if further 'saves' have been made since it was published.

    regards

    marc

  • Nikola 12 posts 132 karma points
    Feb 04, 2019 @ 08:02
    Nikola
    0

    Hello Marc,

    Thanks for responding. I tried your code and I am still getting null back.

    public static string GetLink(IContent item)
    {
      var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
      //Get URL for documents like PDF files
      if (item.ContentType.Alias == "document")
      {
        return item.GetValue<string>("documentLink");
      }
      //Get the URL of published Content of a "unpublished" node
      if (item.HasPublishedVersion && !item.Published)
      {
        var publishedVersion = ApplicationContext.Current.Services.ContentService.GetPublishedVersion(item);
        var typedContentCurrentPublishedVersion = umbracoHelper.TypedContent(publishedVersion.Id);
        return typedContentCurrentPublishedVersion.Url;
      }
      //Standard return URL
      return umbracoHelper.NiceUrl(item.Id);
    }
    

    This line returns null: var typedContentCurrentPublishedVersion = umbracoHelper.TypedContent(publishedVersion.Id);

    In the Code Documentation is written: Returns the content, or null of the content item is not in the cache.

    I recently updated Umbraco to the newest version and I have the fear it's because of that. I already undo the to a older version but and I still got the null. The release might broke something, but i don't think so than everything else works fine.

    EDIT I am pretty sure it's not because of the update

    Thanks in regards!

  • Marc Goodson 2157 posts 14431 karma points MVP 9x c-trib
    Feb 08, 2019 @ 17:17
    Marc Goodson
    100

    Hi Nikola

    I think with this code example I understand better your question.

    Essentially I think you are saying... if the current version of the content item is not yet published - but there is a Previously published version - what is the url of that previously published version?

    If you have the id of a content item, then Umbraco.TypedContent will retrieve whatever the current published version is in the form of an IPublishedContent object, and calling .Url will be that Published Url... however if that current item is UnPublished, then Umbraco.TypedContent will be null...

    Essentially there isn't a way to find out what the Url 'was' of a previously version of a page, if that page is now unpublished.

    I'm not sure of the context of what you are trying to achieve, but on a site, my company built, we had the notion of 'archived content' eg content that was published, but not accessed for a while was automatically 'unpublished' and stored in a flat database table, any future incoming requests for the 'unpublished' page was diverted to serve the archived version. The way we mapped the url to the archived version, was to tap into the Un-publishing event in Umbraco that fires just before something is Unpublished, and use that to find the Url it was published under, storing this in the archive....

Please Sign in or register to post replies

Write your reply to:

Draft