Copied to clipboard

Flag this post as spam?

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


  • Benj Bayfield 4 posts 25 karma points
    Dec 20, 2009 @ 16:54
    Benj Bayfield
    0

    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!

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Dec 20, 2009 @ 22:33
    Lee Kelleher
    0

    Hi Benj,

    You can try using umbraco.library.GetXmlNodeByXPathhttp://our.umbraco.org/wiki/reference/umbracolibrary/getxmlnodebyxpath

    Cheers, Lee.

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Dec 20, 2009 @ 22:39
    Lee Kelleher
    0

    Just realised that the GetXmlNodeByXPath returns an XPathNodeIterator - which isn't really want you want to use in C#/API, so try this instead:

    content.Instance.XmlContent.SelectNodes( xpath )

    It will return an XmlNodeList - which should be more useful to you!

    Cheers, Lee.

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Dec 20, 2009 @ 22:54
    Aaron Powell
    0

    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:

    var ids = "-1,1000,1234".Split(',');
    var node = new Node(int.Parse(ids[2]));

    (Feel free to clean that code up though :P)

  • Benj Bayfield 4 posts 25 karma points
    Dec 20, 2009 @ 23:31
    Benj Bayfield
    0

    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

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Dec 21, 2009 @ 09:14
    Dirk De Grave
    0

    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

  • Jim 18 posts 36 karma points
    Nov 30, 2010 @ 16:07
    Jim
    0

    I am trying to do the same thing and failing misserably!!

    I tried to use

    content.Instance.XmlContent.SelectNodes( xpath )

    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?

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Nov 30, 2010 @ 16:14
    Hendy Racher
    0

    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

  • Jim 18 posts 36 karma points
    Nov 30, 2010 @ 16:36
    Jim
    0

    Thanks for that Hendy. hat works like a charm.

    Do you know if this has implications over published and unpublished items?

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Nov 30, 2010 @ 16:53
    Hendy Racher
    0

    Hi Jim, unfortunately that method will only work with published items as it queries the XML cache.

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Nov 30, 2010 @ 16:58
    Dan Diplo
    0

    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.

  • Jim 18 posts 36 karma points
    Nov 30, 2010 @ 17:04
    Jim
    0

    Hendy, awesome, this is what I want!! :)

    Dan, I came across that also, but cannot find that method, what is the full namespace?!

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Nov 30, 2010 @ 17:24
    Dan Diplo
    0

    Hi Jim. The same expression with the full namespaces would be:

    System.Xml.XmlNodeList nodes = umbraco.content.Instance.XmlContent.SelectNodes("/root//*[@isDoc] [@path='-1,1071,1269']");

     Basically you need to include umbraco namespace and the System.Xml namespace.

  • Jim 18 posts 36 karma points
    Nov 30, 2010 @ 17:37
    Jim
    0

    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.

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Nov 30, 2010 @ 17:38
    Hendy Racher
    0

    Glad it's useful :) btw, that helper method also pulls the xml from umbraco.content.Instance.XmlContent

     

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Nov 30, 2010 @ 18:01
    Dan Diplo
    0

    @Jim - Try adding a reference to the umbraco.dll - I think it's that assembly.

  • Jim 18 posts 36 karma points
    Nov 30, 2010 @ 18:26
    Jim
    0

    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?

Please Sign in or register to post replies

Write your reply to:

Draft