Copied to clipboard

Flag this post as spam?

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


  • Brad 94 posts 151 karma points
    Mar 04, 2013 @ 06:17
    Brad
    0

    Return latest from an unreleted set of items?

    On the site I am working on there is a page that displays all the NewsItems, added from the CMS. Simple stuff.

    However on a totally unrelated page, I want to write a Razor macro that returns just the 'latest' NewsItem.

    Sounds simple enough but I can't for life of me work it out. Every example I read has some sort of 'parent' or 'child' type syntax.

    In my case the NewItems are not related in anyway to the page I want the latest one to appear on. In this example I just want to look at the list of NewItems and get the last one that was entered.

    I'm sure this is simple. I've just not done it before in this context and it has be stumped.

    Cheers.

  • Rich Green 2246 posts 4008 karma points
    Mar 04, 2013 @ 08:00
    Rich Green
    0

    Hey Brad,

    I don't have the Razor solution off the top of my head but basically you navigate (within Razor) to the 'top' of the site and then back down to all the 'NewsItems'.

    I'll dig out some code when I get to the office.

    Rich

  • gary 385 posts 916 karma points
    Mar 04, 2013 @ 10:20
    gary
    1

    Hi Brad

    For V6 Umbraco Mvc, looks like this -

    var get = @CurrentPage.AncestorOrSelf().Descendants("InsertNameOfYourDocumentType").OrderBy("UpdateDate desc");

    @foreach ( var item in get.Take(1))

    { <a href="@item.Url" >@item.Name"</a>}

    This will do just as Rich says - from any page - climb to the root node, then go down to find the descendant of "YourDocumentType", sort them into date order and then will for each document only "take" one giving the name of the latest document in a link.

    Depending on your vesion of Umbraco, or how you may want to work, you can split this up - or may have to tweak for webforms razor.

    Hope it is useful

    G

     

     

Please Sign in or register to post replies

Write your reply to:

Draft