Copied to clipboard

Flag this post as spam?

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


  • Fredrik Esseen 610 posts 906 karma points
    Aug 24, 2015 @ 09:10
    Fredrik Esseen
    0

    Get node with certain name

    Hi all!

    I have a solution where I try to match a node with a name from another db.

    I have a tree with subpages and multiple levels and I want to traverse the whole tree and find a node with a certain name.

    Im trying to use the right syntax to search all nodes. The root node is a separate tree so I've hardcoded to the Id:

    var home = Umbraco.Content(1957); foreach (var childPage in home.Descendants.Where("NodeTypeAlias == @0 && Name = @1", "Product","The dynamic name im trying to find"){}
    

    But im not getting any result?

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Aug 24, 2015 @ 10:06
    Lee Kelleher
    101

    Hi froad,

    I know it's not very "Linq-y", but for this I'd try using an XPath query.

    Something like this...

    var homeId = 1957;
    var docTypeAlias = "Product";
    var dynamicName = "The dynamic name im trying to find";
    var xpath = string.Format("id({0})//{1}[@isDoc and @nodeName = '{2}']", homeId, docTypeAlias, dynamicName);
    var nodes = Umbraco.TypedContentAtXPath(xpath);
    
    foreach (var node in nodes)
    {
        // do something
    }
    

    Just in case you didn't know about the id() bit ... it is quite powerfully, it's a super-fast way of returning a node :-)

    I hope this helps?

    Cheers,
    - Lee

  • Fredrik Esseen 610 posts 906 karma points
    Aug 24, 2015 @ 12:53
    Fredrik Esseen
    0

    That worked perfectly! Thx!

Please Sign in or register to post replies

Write your reply to:

Draft