Copied to clipboard

Flag this post as spam?

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


  • Tom 10 posts 62 karma points
    Mar 19, 2013 @ 21:51
    Tom
    0

    Get latest 10 content items from all nodes in tree without a loop

    Hi,

    I am just creating my first Umbraco site and am looking to create an RSS feed that displays the latest 10 items from the site (of various content types) from various levels and various nodes across the content tree. e.g. checking all descendants from the root node and returning the 10 with the most recent createDate.

    Is there anyway to do this without a loop? I don't want to use a loop as when the site has 1000's of pages I would think this would be a slow process and would need an additional sorting algorithm.

    e.g can the Umbraco Library do an equivalent of a SQL query like 

    SELECT TOP 10 *
    FROM Content -- or whatever this is called in Umbraco
    ORDER BY CreateDate DESC

    I have seen the Razor RSS cookbook example at 

    http://umbraco.com/help-and-support/video-tutorials/umbraco-fundamentals/razor-recipes/rss.aspx ;

    but this doesn't seem to span more than one level or multiple sibling nodes

    p.s. as a new user I am loving the flexibility of creating your own document types in Umbraco :0)

    I am using Umbraco v 6.02

    Thanks.

  • Funka! 398 posts 661 karma points
    Mar 19, 2013 @ 23:45
    Funka!
    0

    My first inclination would be for you to use an XPath query (yes, in Razor, not xslt) and then call OrderBy() and then Take(). You can put this into a macro and cache it for a minute or so. Even with 1000's of nodes my gut tells me this would still be pretty fast, but really the only way to know is to test it, try it, and measure it.This is all off the cuff, but something like this:

    var latestThings = Model.AncestorOrSelf().XPath(".//someNodeTypeAlias").Where("Visible").OrderBy("CreateDate desc").Take(10);
    foreach (var node in latestThings)
    {
         ...
    }

    Best of luck!

  • Tom 10 posts 62 karma points
    Mar 20, 2013 @ 10:57
    Tom
    0

    Thanks for the reply, what if I want to collect all node types, e.g. article, articlecategory etc. What would I replace .//someNodeTypeAlias with?

    thanks.

  • Tom 10 posts 62 karma points
    Mar 20, 2013 @ 14:21
    Tom
    100

    The code above has successfully worked a single document type. Thanks. 

    I have modified the code to select both Article and ArticleCategory document types. I am a bit of an XPath novice so don't know if this is the best / correct way to do this? Ideally I would like to expand this to include other document types.

    My query currently reads

    var latestThings = Model.AncestorOrSelf().XPath(".//Article|.//ArticleCategory").Where("Visible").OrderBy("CreateDate desc").Take(10);

  • Funka! 398 posts 661 karma points
    Mar 20, 2013 @ 22:30
    Funka!
    0

    Yes, i frequently use that same OR separator in the XPath query to do as you achieve. Although I have been traditionally putting a space around the vertical pipe character, didn't realize it works the same without. Glad you got this working!

  • Funka! 398 posts 661 karma points
    Mar 20, 2013 @ 22:33
    Funka!
    0

    This is one of my favorite references: glad that is hasn't gone 404 on us yet! http://msdn.microsoft.com/en-us/library/ms256086.aspx

Please Sign in or register to post replies

Write your reply to:

Draft