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 590 posts 806 karma points
    Jan 26, 2017 @ 11:43
    Fredrik Esseen

    Getting all nodes of certain doctype in controller

    Im going nuts here trying to find a way getting all nodes under a single node that is of a certain doc type!

    The nodes is on level 3 and Im not getting it to work. This is what I have so far:

      Umbraco.Core.Models.IPublishedContent baseNode = Umbraco.Content(1102);
            var theNodes = baseNode.Children.Where(x => x.DocumentTypeAlias == "myNodeDocType" && x.Level == 3).OrderBy(n => n.CreateDate);

    I know I cant use .Children as it only gets the first level but I cant get Descendants() to work.

    I must be using the wrong class or something..?

  • Marc Goodson 1173 posts 7865 karma points MVP 5x c-trib
    Jan 28, 2017 @ 14:13
    Marc Goodson

    Hi Frederic


    I can't work out whether you want to return all nodes of a particular document type underneath your baseNode, eg children of children of children at many depths, or if the nodes are all children of the base node.

    If your nodes are all children of your base node then you kind of have the right approach, but there is no need to specify the level, Children (which accepts a predicate no need to add Where()) will only return nodes directly underneath your base.

     var theNodes = baseNode.Children(f => f.DocumentTypeAlias == "myNodeDocType").OrderBy(f => f.CreateDate);

    There is also an overload that accepts a list of document type alias:

      var theNodes = baseNode.Children(new string[] { "myNodeDocType" }).OrderBy(f => f.CreateDate);

    With descendants (which depending on your structure may not be an efficient way to go) the syntax is slighly different, there is an overload for Doc Type Alias or for Level but no predicate:

    var theNodes = baseNode.Descendants("myNodeDocType").Where(f => f.Level == 3).OrderBy(f => f.CreateDate);

    This will load all descendants of the specific type into memory, and then filter by level... so depending on your structure this might be a better overload:

     var theNodes = baseNode.Descendants(3).Where(f => f.DocumentTypeAlias == "myNodeDocType").OrderBy(f => f.CreateDate);

    this will get all the descendants at depth of 3, and then filter in memory by document type...

    both of these approaches will be more efficient than:

     var theNodes2 = baseNode.Descendants().Where(f => f.DocumentTypeAlias == "myNodeDocType" && .f.Level == 3).OrderBy(f => f.CreateDate);

    Finally from a performance perspective if you've hundreds of descendants the the old fashioned XPath approach is going to be the most efficient way eg:

     var nodes = Umbraco.TypedContentAtXPath("//BaseNodeAlias[@id=1102]//myNodeDocType[@level=3]");
  • Fredrik Esseen 590 posts 806 karma points
    Jan 30, 2017 @ 07:35
    Fredrik Esseen

    I want to get all nodes of a certain doc type no matter what level the node is on, but the problem is that Im not allowed to use Descendants()

    On the baseNode variable I dont get the property "Descendants()". Am i using the wrong dlls?

    using Umbraco.Web.Mvc; using Umbraco.Core; using Umbraco; using Umbraco.Web.UI; using Umbraco.Core.Security;

    The error I get is: "Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'Descendants' and no extension method 'Descendants' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?"

  • Søren Kottal 484 posts 3157 karma points MVP 2x c-trib
    Jan 30, 2017 @ 07:55
    Søren Kottal

    Try adding

    using Umbraco.Web.Models
  • Fredrik Esseen 590 posts 806 karma points
    Jan 30, 2017 @ 08:04
    Fredrik Esseen

    Same error appears.

Please Sign in or register to post replies

Write your reply to: