Copied to clipboard

Flag this post as spam?

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


  • BillyTheKid 3 posts 105 karma points
    Aug 11, 2015 @ 06:39
    BillyTheKid
    1

    Getting Descendants from Root node

    I'm trying to get all descendants of the rootnode (or any node for that matter) from my UmbracoApiController, filtered with Linq.

    So basicly what I would want to do is:

    rootnode.Descendants(a => a.DocumentTypeAlias == "myDocType" && a.Name == "myName")
    

    This is documented at: https://our.umbraco.org/Documentation/Implementation/Routing/MVC/querying

    But somehow, when I select the rootnode form the UmbracoApiController, my IPublishedContent object doesn't have a Descendants() method.

    var root = Umbraco.TypedContentAtRoot().First();
    

    It does have a Children property, but obviously this only returns all the direct children, and not all descendants.

    How else am I able to select all documents, filtered by DocumentTypeAlias, and probably also by other properties (CreatedAt for example)?

  • Kevin Jump 2348 posts 14896 karma points MVP 8x c-trib
    Aug 11, 2015 @ 10:57
    Kevin Jump
    1

    Hi

    it should have decendants ...

    root.Descendants().Where(x => x.DocumentTypeAlias == "someDocType"); 
    

    things to check.

    1. Visual studio 2015 - doesn't do intellisense properly for Umbraco (pre v7.3) - it's an MVC4 / 5 thing, but that might be why you can't see it?

    2. Inheritance - my views are using

      @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    if you're inheriting from a model again Intellisense (and the site) might not work.

    however, if you want to do things sitewide, i would say have a look at examine, getting nodes of type across a whole site can take some time, using examine is slightly more complex but way faster.

    failing that cache the macro or partial view because that will make it quick too.

  • BillyTheKid 3 posts 105 karma points
    Aug 11, 2015 @ 11:57
    BillyTheKid
    101

    I do using Visual Studio 2015, but it's not compiling, so IDE should'nt matter.

    I managed to fix this however by using the UmbracoHelper

    var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    var blogs = umbracoHelper.TypedContentAtRoot().First().Descendants("Blog").ToList();
    
  • Matty 34 posts 148 karma points
    Aug 30, 2018 @ 04:11
    Matty
    1

    I ran into the same issue, turns out I was missing a using statement. I had

    using Umbraco.Web.Mvc;
    

    But also needed to have

    using Umbraco.Web;
    

    Then the descendants option showed up.

  • 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