Umbraco - Get Node by ID programmatically
Asked Answered
R

3

24

Running Umbraco 4x I am creating a helper method in C# that I can recursively call to create child categories of a particular node (category).

The method takes a parentNodeID as a parameter. I need to retrieve the properties of that parent node. I know I can use the static method Node.GetCurrent() but I'm looking for something like Node.GetNodeById(parentNodeID).

I just can't see where this method lives. I know there is the umbraco.library.getNodeXMLbyId method, but does that give me the name property of the node?

Me Umbraco N00b :)

Rajewski answered 10/6, 2009 at 10:7 Comment(0)
P
42

You can just do

var node = new Node(nodeId).

Took me a while to find it too!

Pedroza answered 10/6, 2009 at 10:26 Comment(4)
Wicked. I knew it had to be something simple like that. Cheer Sam!Rajewski
Yes, Umbraco's choice of using constructors to return existing persisted instances is rather odd, and I would say... unusual.Byrd
+1 for both. It helped me. I also needed to add using umbraco.NodeFactory;Pemberton
Remember also to use Document instead, if you need to alter the node (our.umbraco.org/wiki/reference/api-cheatsheet/…).Kipper
R
1

Use this

umbraco.NodeFactory.Node headerNode = uQuery.GetNode(NodeId);

add namespace

using umbraco.NodeFactory;
Ruche answered 10/2, 2014 at 14:20 Comment(0)
D
0

You can also do

Document doc = new Document(nodeId)

This works the same as Node but gets the values straight from the database instead of the XML cache. Use this if you're going to be updating the documents property values.

Dorman answered 1/2, 2013 at 15:3 Comment(2)
But be aware that for front end reading, you should always use the published XML node.Lupelupee
If you do this in Umbraco 7 you will get the warning - 'umbraco.cms.businesslogic.web.Document' is obsolete: 'Obsolete, Use Umbraco.Core.Models.Content'Hedvah

© 2022 - 2024 — McMap. All rights reserved.