Copied to clipboard

Flag this post as spam?

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


  • Bobby Davro 2 posts 22 karma points
    Dec 03, 2014 @ 11:49
    Bobby Davro
    0

    How to get at all child documents of some documents at specific level

    My content structure is thus:

    Home

     - Static Pages

        - Static Page 1

     - News

       - News Page 1

     - Comment

      - Comment Page 1

    Using a partial view I'd like to pick the top 9 articles (ordered by published date / updated date) from either News and Comment. News and Comment share a common document type Articles - their children a common doc type Article. Staic Pages have their own content type.

    What's the easiest way. Do I start with @CurrentPage or the content query object? Any snippets out there (guessing this has been done a zillion times, the e.g.s I have found are too simplistic)?

  • Dan Lister 416 posts 1973 karma points c-trib
    Dec 03, 2014 @ 12:01
    Dan Lister
    0

    Hi Bobby,

    You could try using XPath to find all articles at a specific level. I haven't tested it but it should point you in the right direction:

    var articles = Umbraco.TypedContentAtXPath("//Article[@level='2']").Take(9);
    

    Hope that helps.

    Thanks, Dan.

  • Bobby Davro 2 posts 22 karma points
    Dec 03, 2014 @ 12:11
    Bobby Davro
    0

    Hi

    Good answer thanks.

    I'd already managed to get there (found a page on strongly types object model) - I am more comfortable with as few "magic strings" as possible, I prefer this:

    const int perPage = 9;
    var currentPage = Model.Content;
    var pages = currentPage.Descendants("Article")
            .Where(x => x.IsVisible())
            .OrderByDescending(x => x.UpdateDate)
            .Take(perPage)
            .ToList();
    

    I guess it's a matter of preference - ultimately both methods end up as the same sql query?

  • 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