Copied to clipboard

Flag this post as spam?

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


  • Tizer 170 posts 201 karma points
    Jul 11, 2013 @ 08:58
    Tizer
    0

    Help Selecting all these nodes with Razor

    Here is my content tree at the moment:

    Content
    - Level 1
    - - Level 2
    - - - Level 3
    - - - - Level 4
    - - - - Level 4
    - - - Level 3
    - - - - Level 4
    - - - - Level 4

    I am wanting to select data from all the Level 4 nodes (they are the same Doctype) and have been getting confused :)

    This is where I am at the moment, which selects the first set only - how do I get all of them?

    @using umbraco.MacroEngines;
    @inherits umbraco.MacroEngines.DynamicNodeContext

    @{
    var r = new Random();
    var numberOfItems = 1;
    var root = Model.AncestorOrSelf("Level4");
    var tip = root.DocType3.First();
    List<DynamicNode> pages = new DynamicNode(tip.Id).Descendants().Items.Where(x => x.Visible).OrderBy(x => r.Next()).Take(numberOfItems).ToList();
    }
  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jul 11, 2013 @ 10:47
    Jeavon Leopold
    0

    Hi Tizer,

    I would use the XPath method, e.g.

    var pages = new DynamicNode(Model.AncestorOrSelf(1)).XPath("//Level4").Where((x => x.Visible));

     or if you want to be more specific and select only Level4 docs that are children of Level3 docs then:

    var pages = new DynamicNode(Model.AncestorOrSelf(1)).XPath("//Level3/Level4").Where((x => x.Visible));

     Thanks,

    Jeavon

  • Tizer 170 posts 201 karma points
    Jul 12, 2013 @ 03:42
    Tizer
    0

    Thanks Jeavon - works perfectly :)

    Xpath is great - I really need to get up to speed with it, I always forget about it

  • Tizer 170 posts 201 karma points
    Jul 12, 2013 @ 03:43
    Tizer
    0

    Thanks Jeavon - works perfectly :)

    Xpath is great - I really need to get up to speed with it, I always forget about it

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jul 15, 2013 @ 10:45
    Jeavon Leopold
    0

    Brilliant, also we now have XPath as a option for getting collections when using Mvc as some new methods such as Umbraco.TypedContentAtXPath were introduced in v6.1 so it's worth thinking about.

Please Sign in or register to post replies

Write your reply to:

Draft