Copied to clipboard

Flag this post as spam?

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


  • BarrySlisk 15 posts 35 karma points
    Aug 01, 2012 @ 10:42
    BarrySlisk
    0

    Select node by path

    I want to select the Data node in umbraco tree below using the path.

    Content

         Data

     

    I could be anywhere in the three.

     

    I have tried among other things something like Model.XPath("/Data").FirstOrDefault() but that does not work.

     

     

     

     

  • Ben Norman 167 posts 276 karma points
    Aug 01, 2012 @ 10:45
    Ben Norman
    0

    Hey Barry,

    You could get that using Model.AncestorOrSelf(). See how that goes

  • BarrySlisk 15 posts 35 karma points
    Aug 01, 2012 @ 10:50
    BarrySlisk
    0

    Could you be more specific. My current node might not be a decendant.

  • Ben Norman 167 posts 276 karma points
    Aug 01, 2012 @ 11:03
    Ben Norman
    0

    One trick I use is to put a content picker property on the Model.AncestorOrSelf() Node which can be used to point to the Data Node

    eg: Property Alias: DataRepository

    and then use

    var repo = Model.NodeById(Model.AncestorOrSelf().DataRepository)

    Will that get you working?

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Aug 01, 2012 @ 11:04
    Hendy Racher
    0

    Hi Barry,

    If you want to select all instances of 'Data' nodes how about:

    IEnumerable<Node> dataNodes = uQuery.GetNodesByXPath("//Data[@isDoc]");

    or

    IEnumerable<Node> dataNodes = uQuery.GetNodesByType("Data");

    HTH,

    Hendy

  • BarrySlisk 15 posts 35 karma points
    Aug 01, 2012 @ 11:13
    BarrySlisk
    0

    @Hendy Racher

    So I have to install some other component (UQuery) in order to select a node in the content tree? Am I understanding that right? There is no simple out of the box way of doing it?

    Your xpath example does not start from root node, so how would that work if node I am looking for os not below current node?

     

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Aug 01, 2012 @ 11:18
    Hendy Racher
    0

    Hi Barry,

    uQuery is available in Umbraco 4.8, or if you're using an older version then uComponents would be required.

    The above XPath will start at the root (to start from the current node you could use the $currentPage token in the XPath expression).

  • BarrySlisk 15 posts 35 karma points
    Aug 01, 2012 @ 11:46
    BarrySlisk
    0

    I'm using 4.8, so I'll give it a go.

  • BarrySlisk 15 posts 35 karma points
    Aug 01, 2012 @ 12:03
    BarrySlisk
    0

    'umbraco.MacroEngines.BaseContext.Node' is a 'property' but is used like a 'type'

    If I add:

    using uComponents.Core;

    The type or namespace name 'uComponents' could not be found (are you missing a using directive or an assembly reference?)

     

  • BarrySlisk 15 posts 35 karma points
    Aug 01, 2012 @ 12:24
    BarrySlisk
    0

    @using umbraco.MacroEngines;
    @using umbraco;


    @{
    var dataNode = uQuery.GetNodesByXPath("//Data[@isDoc]").FirstOrDefault();
    if (dataNode == null) {
       

    node not found


    }
    else
    {
       

    node found


    }
    }

     

    This outputs: node not found. The Data node is located under "Content".

     

    NOTE: after I edited this post it changed changed my h3 tags but you get the idea.

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Aug 01, 2012 @ 13:37
    Hendy Racher
    0

    Hi Barry, what's the alias of the 'Data' node ? is it 'Data' or 'data' - are there 'Data' xml nodes in your /App_Data/umbraco.config file ?

  • BarrySlisk 15 posts 35 karma points
    Aug 01, 2012 @ 13:49
    BarrySlisk
    0

    First two lines from umbraco.config look like this:

    <root id="-1">
      <DocDataFolder id="1049" parentID="-1" level="1" writerID="0" creatorID="0" nodeType="1048" template="0" sortOrder="3" createDate="2012-07-31T16:09:12" updateDate="2012-07-31T16:13:21" nodeName="Data" urlName="data" writerName="admin" creatorName="admin" path="-1,1049" isDoc="">

  • BarrySlisk 15 posts 35 karma points
    Aug 01, 2012 @ 13:52
    BarrySlisk
    0

     

    @uQuery.GetRootNode().Children[0].Name; // THIS ONE RETURNS "Data". But this is too hardcoded.
           
    @uQuery.GetRootNode().Children.Where("Name == \"Data\""); // THIS ONE CRASHES SCRIPT

    @uQuery.GetRootNode().Children.Where("x => x.Name == \"Data\""); // THIS ONE CRASHES SCRIPT

    @uQuery.GetRootNode().Children.Where("x => x.Name == 'Data'"); // THIS ONE CRASHES SCRIPT

     

     

     

     

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Aug 01, 2012 @ 13:54
    Hendy Racher
    0

    Arrh, that'd explain why the XPath expression didn't return anything - from the above, the node type is "DocDataFolder" which has a name of "Data".

    Do you want to retrieve all nodes called "Data" or all nodes using the document type "DocDataFolder" ?

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Aug 01, 2012 @ 13:57
    Hendy Racher
    0

    BTW, in the above example where the script crashes, this is because the Linq Where method will expect a lambda expression rather than a string.

    Does the following help at all ?

    Node dataNode = uQuery.GetNodesByName("Data").FirstOrDefault();
  • BarrySlisk 15 posts 35 karma points
    Aug 01, 2012 @ 14:02
    BarrySlisk
    0

    I want to retrieve the node by the name "Data" located under the root node. I know there is only one under content.

  • BarrySlisk 15 posts 35 karma points
    Aug 01, 2012 @ 14:06
    BarrySlisk
    0

    Yes, your last example did return the Data node although if another node with the name "Data" existed somewhere in tree then that would be returned as well right?

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Aug 01, 2012 @ 14:20
    Hendy Racher
    0

    The above should do the trick - GetNodesByName() will be converted into an XPath expression so should be fast, but it will find all Data nodes thoughout the site and take the first one, to ensure you get a node called Data that sits immediately beneath the root you could try:

    Node dataNode = uQuery.GetRootNode().GetChildNode().Where(x => x.Name == "Data").FirstOrDefault();

    (or use a suitable XPath expression)

  • BarrySlisk 15 posts 35 karma points
    Aug 01, 2012 @ 14:25
    BarrySlisk
    0

    Both

            var temp = uQuery.GetRootNode().GetChildNode().Where(x => x.Name == "Data").FirstOrDefault();


    And

           var temp = uQuery.GetRootNode().GetChildNode().Where("Name == \"Data\"").FirstOrDefault();

    AND

           var temp = uQuery.GetRootNode().Children.Where(x => x.Name == "Data").FirstOrDefault();

     

    Crashes script.

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Aug 01, 2012 @ 14:30
    Hendy Racher
    0

    sorry, a typo, should be GetChildNodes()

    uQuery.GetRootNode().GetChildNodes().Where(x => x.Name == "Data").FirstOrDefault();
  • BarrySlisk 15 posts 35 karma points
    Aug 01, 2012 @ 15:01
    BarrySlisk
    0

    YES, that worked. In the meantime I also got this one to work:

    var temp = uQuery.GetNodesByXPath("//DocDataFolder[@nodeName='Data']").FirstOrDefault();

    I like your method better though. Thank you for your help!

     

     

Please Sign in or register to post replies

Write your reply to:

Draft