Copied to clipboard

Flag this post as spam?

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


  • Saif Obeidat 79 posts 296 karma points
    Aug 03, 2019 @ 09:46
    Saif Obeidat
    0

    How to to get all nodes under a specific parent - Examine | Umbraco8

    Hi all,

    I have this structure :

    • Parent1

      • 2019
        .Node1
        .Node2
      • 2018
        .Node3
        .Node4
    • Parent2

      • 2019
        .Node
        .Node
      • 2018
        .Node
        .Node

    I need to know, how to get all nodes that are under Parent1

    so the result that i need is [node1,node2,node3,node4]

    It would be appreciated if someone help me in this.

  • Matthew Taylor 11 posts 85 karma points
    Aug 09, 2019 @ 12:35
    Matthew Taylor
    0

    Which version of Umbraco are you using?

    Are you trying to display this on Parent 1 or from another node?

    Did you only want to display this on Parent 1, but not Parent 2?

    Here are some generic examples, but if you give me more info I can give you the exact code you need to target the right nodes.

    @{
        // Start from the home node
        var homeNode = Model.Root();
    }
    
    @* Take anything under the home node that is visible *@
    @foreach (var item in homeNode.Children.Where(x => x.IsVisible()))
    {
    
    }
    
    @* Take the descendants of the home node with a document type alias of yourDocTypeAlias that is visible *@
    @foreach (var item in homeNode.Descendants("yourDocTypeAlias").Where(x => x.IsVisible()))
    {
    
    }
    
    @{
        // Start from the Current Page
        var CurrentPage = Model;
    }
    
    @* Take anything under the current page that is visible *@
    @foreach (var item in CurrentPage.Children.Where(x => x.IsVisible()))
    {
    
    }
    
    @* Take the descendants of the current page with a document type alias of yourDocTypeAlias that is visible *@
    @foreach (var item in CurrentPage.Descendants("yourDocTypeAlias").Where(x => x.IsVisible()))
    {
    
    }
    
  • Saif Obeidat 79 posts 296 karma points
    Aug 20, 2019 @ 07:40
    Saif Obeidat
    101

    I've found a quick way to solve my question:

    results = searcher.CreateQuery()
                                .Field("path", Model.Path.ToString().MultipleCharacterWildcard()).And()
    

    This code will get only nodes under a specific path.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies