Copied to clipboard

Flag this post as spam?

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


  • Halldór Hrafn 15 posts 72 karma points
    Apr 19, 2011 @ 11:21
    Halldór Hrafn
    0

    Querying by document type (and child types) recursively

    Hi,

    I have a sort of a article setup, where I have a Article document type, and I create those articls in Date folders (auto-generated folders based on a date value).

    But I also have child types derived from this article type, for example review is a child type of article. Thought I'd be smart and have this set up like so, so I could both list based on the parten type, and differentiate on the types.

    So I could have a tree like this:

    Home´
    - 2010
    - 11
    - Some Article
    - Another Article
    - A Review of something (kinda like a article)
    - 12
    - Yet anohter Article
    - Anohter silly Review

    Let's that I want to list newsest 5 Articles (including child types). Also I'd like to just list by exact type (just the Articles and just the Reviews etc.)

    Any nifty ideas ? :)

     

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Apr 19, 2011 @ 11:43
    Sebastiaan Janssen
    1

    Something like this:

    @Model.AncestorOrSelf(1).DescendantsOrSelf("ArticleTypeAlias").OrderBy("CreateDate desc").Take(5)

    This will give you all of the articles.

    But you can't get descendants of different node types. For that I would just put them in a generic list and do a for-each on the list:

       @{
            var myList = new List<umbraco.MacroEngines.DynamicNode>();
            foreach (dynamic page in @Model.AncestorOrSelf(1).DescendantsOrSelf().OrderBy("CreateDate desc")) {
                if(page.NodeTypeAlias == "OneType" || page.NodeTypeAlias == "OtherType")
                {
                    myList.Add(page);
                }
            }
        }
    
        @foreach(dynamic page in myList.Take(5)) {
            <span>@page.Name</span>
        }
  • Halldór Hrafn 15 posts 72 karma points
    Apr 19, 2011 @ 11:55
    Halldór Hrafn
    0

    Thanks a million, this is exactly what I needed to get a kick start! :D

Please Sign in or register to post replies

Write your reply to:

Draft