check property existence
Asked Answered
W

3

11

In umbraco, is it possible to check if certain property exist for particular node?

For example, I am having 5 nodes and I am having media picker property for first node alone. I need to check all the nodes whether media picker property is available for nodes or not?

Wellman answered 4/6, 2010 at 12:23 Comment(1)
Are you doing this in an xslt file or in a user control?Haphazard
H
11

I think you can just check property existence by comparing to null:

Node somenode = new Node(myNodeID);
if (somenode.GetProperty("myProperty") != null)
{
   string myProperty = somenode.GetProperty("myProperty").Value.ToString();
   //Do something with myProperty
}
Haphazard answered 8/6, 2010 at 21:39 Comment(0)
L
9

If you are using Razor you can do it like this:

Model.HasProperty("MyPropertyAlias")

And you can check if the property contains a value as follows:

Model.HasValue("MyPropertyAlias")
Lonilonier answered 23/3, 2013 at 7:22 Comment(0)
H
0

you can do like this

 if (Model.Content.HasValue("alias"))
 {
//placeyour code here
 }
Hadrian answered 26/9, 2014 at 13:50 Comment(1)
You should first check that the property exists before checking for a value or I think you will get a null reference exception if for some reason it doesn't exist.Lonilonier

© 2022 - 2024 — McMap. All rights reserved.