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.
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'.
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.
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.
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
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
is working on a reply...