Copied to clipboard

Flag this post as spam?

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


  • Osman Coskun 164 posts 398 karma points
    Dec 05, 2017 @ 06:35
    Osman Coskun
    0

    How to get the URL from the content picker on the root document?

    Hello, I've a content picker 2 property editor with the alias umbracoInternalRedirectId in the root document to specify the home page of web site. When i try to get the URL of home page in navigation macro i get error with the following code:

    var site = Model.Content.AncestorOrSelf(1);
    var homeLink = "";
    if (site.HasProperty("umbracoInternalRedirectId"))
    {
        IPublishedContent homeNode = site.GetPropertyValue<IPublishedContent>("umbracoInternalRedirectId");
        homeLink = homeNode.Url;
    }
    

    The error i get is : Exception: System.NullReferenceException: Object reference not set to an instance of an object.

    Any helps please?

    UPDATE: When i test the same process with the alias test it works? Is there any special issue with the alias umbracoInternalRedirectId ?

  • Matt Darby 28 posts 391 karma points c-trib
    Dec 05, 2017 @ 11:22
    Matt Darby
    1

    Hi Osman,

    .HasProperty only checks if the content has a property with that alias. So it's possible the picker value is not set (so it's null) and then causes this error on homeNode.Url.

    Check for null like so:

    IPublishedContent homeNode = site.GetPropertyValue<IPublishedContent>("umbracoInternalRedirectId");
    
    if (homeNode != null)
    {
        <div>@homeNode.Url</div>
    }
    

    If it isn't that - what version of Umbraco are you using? There were issues with ContentPicker2 and umbracoInternalRedirectId in previous versions, see the discussion here.

  • Osman Coskun 164 posts 398 karma points
    Dec 05, 2017 @ 11:39
    Osman Coskun
    0

    Hi Matt,

    I checked homeNode against null. Even content picker has value umbracoInternalRedirectId, homeNode seems to be null.

    I'm working with Umbraco version 7.7.4.

    More interesting, i can get UDI ( umb://document/1f54a39b17e248e484b93a8916805398 ) with the code below;

    var home = Model.Content.AncestorOrSelf(1);
    var homeNode = home.GetPropertyValue("umbracoInternalRedirectId");
        if(homeNode != null){
            <div>@homeNode</div>
        }
    
  • Andy Cox 31 posts 182 karma points
    Oct 15, 2019 @ 08:57
    Andy Cox
    0

    I know this is an old issue but just as a note incase anyone else encounters this issue...

    The property "umbracoInternalRedirectId" is excluded when either the content or multinode picker is converted. That's why the property value is returning null. This is presumably to avoid recursion when converting???

    So the best method, I've found, is to use Osman's suggestion to get the UDI then get the published content using that UDI.

Please Sign in or register to post replies

Write your reply to:

Draft