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 ?
.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.
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.
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>
}
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.
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:
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 ?
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 onhomeNode.Url
.Check for null like so:
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.
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;
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.
is working on a reply...