On my second umbraco installation. Maybe I'm missing something obvious, but is there a way to get a node object in the API from the path of the item, or do I have to use the Id? It's pretty easy with XPath in XSLT, but I can't find a method in the API.
I was referring to the path in the hierarchy as Lee suggests, and I'm more than happy with XPath so I'll try that route.
It does strike me as a little odd that there isn't a GetNode(string path) method of the Node class. Surely the engine must have to perform a similar process to determine the context node from the URL.
Your assumption is correct, if you'd like to know how umbraco fetches the correct page, add ?umbdebugshowtrace=true to your url and look for 'umbracoRequestHandler' category entries in the trace.
I've found this works in Umbraco 4.5.2 to fetch nodes by path:
XmlNodeList nodes = content.Instance.XmlContent.SelectNodes("/root//*[@isDoc] [@path='-1,1071,1269']"); string name = nodes[0].Attributes["nodeName"].InnerText;
It fetches all nodes that match the xpath and then takes the first node (using the square-bracket indexer) and gets the value of the nodeName attribute.
I am coding in VB (not my prefered choice as I am C# dev at heart)
As the XmlContent has replaced xmlContent (notice the case difference) VB cannot distinguish between the properties as it is a silly case insensitive language, so refuses to show me the property at all!! (Thanks VB!!)
Does anyone know of a way to make VB pick the correct property? or be case sensitive?
Get node by path
Hi,
On my second umbraco installation. Maybe I'm missing something obvious, but is there a way to get a node object in the API from the path of the item, or do I have to use the Id? It's pretty easy with XPath in XSLT, but I can't find a method in the API.
Thanks!
Hi Benj,
You can try using umbraco.library.GetXmlNodeByXPath: http://our.umbraco.org/wiki/reference/umbracolibrary/getxmlnodebyxpath
Cheers, Lee.
Just realised that the GetXmlNodeByXPath returns an XPathNodeIterator - which isn't really want you want to use in C#/API, so try this instead:
It will return an XmlNodeList - which should be more useful to you!
Cheers, Lee.
If you're referring to path which is like -1,1000,1234 then I believe the only way to get it is via string split and use the ID, eg:
(Feel free to clean that code up though :P)
Thanks guys,
I was referring to the path in the hierarchy as Lee suggests, and I'm more than happy with XPath so I'll try that route.
It does strike me as a little odd that there isn't a GetNode(string path) method of the Node class. Surely the engine must have to perform a similar process to determine the context node from the URL.
Anyway, I'm much obliged and will press on!
Cheers,
Benj
Hi Benj,
Your assumption is correct, if you'd like to know how umbraco fetches the correct page, add ?umbdebugshowtrace=true to your url and look for 'umbracoRequestHandler' category entries in the trace.
Cheers,
/Dirk
I am trying to do the same thing and failing misserably!!
I tried to use
but this method does not exist in 4.5 (at least I cannot find it)
This should be such a simple task, yet I can't seem to find anything on it?!
Can someone please help?
Hi Jim,
If you want to return a collection of Nodes from an XPath expression, there's a GetNodesFromXPath() method in this Helper Class.
HTH,
Hendy
Thanks for that Hendy. hat works like a charm.
Do you know if this has implications over published and unpublished items?
Hi Jim, unfortunately that method will only work with published items as it queries the XML cache.
I've found this works in Umbraco 4.5.2 to fetch nodes by path:
It fetches all nodes that match the xpath and then takes the first node (using the square-bracket indexer) and gets the value of the nodeName attribute.
Hendy, awesome, this is what I want!! :)
Dan, I came across that also, but cannot find that method, what is the full namespace?!
Hi Jim. The same expression with the full namespaces would be:
Basically you need to include umbraco namespace and the System.Xml namespace.
I tried that!!!
My content.Instance does not have XmlContent as a class!!
Am I using the wrong reference?
Added cms.dll and the businesslogic one.
Glad it's useful :) btw, that helper method also pulls the xml from umbraco.content.Instance.XmlContent
@Jim - Try adding a reference to the umbraco.dll - I think it's that assembly.
I am coding in VB (not my prefered choice as I am C# dev at heart)
As the XmlContent has replaced xmlContent (notice the case difference) VB cannot distinguish between the properties as it is a silly case insensitive language, so refuses to show me the property at all!! (Thanks VB!!)
Does anyone know of a way to make VB pick the correct property? or be case sensitive?
is working on a reply...