I have an Umbraco installation which maintains a number of websites. Each website has a news section. I need to select the top / latest 4 news items from the various websites, each website can only provide one news item, so some websites will not provide any (because they are not in the 4 most recent). What is the best way to do that with Razor?
I am using Umbraco V6 (not MVC).
So, I have something like:
website A
News
News A1
...
News AN
website B
News
News B1
...
News BN
etc
At the moment, I cannot guarantee that the latest news item is in any particular place in the list (i.e. first or last), but probably could do if that would help.
I'm going to assume you can use a Partial View Macro and have Umbraco v6.1+ (if not we can come up with something different). So assuming all news articles are of the same document type in all websites you can use TypedContentAtXPath to select them all, e.g.
var allNewsItems = Umbraco.TypedContentAtXPath("//NewsItemsDocTypAlais").OrderBy(x => x.GetPropertyValue<Date>("aliasOfYourDateToSortBy")).Take(4);
Is Umbraco.TypedContentAtRoot an MVC thing, as it doesn't seem to work in my Razor macroscript? (Using v6.1.6 and webforms, not MVC)
Other than that, your solution is pretty much what I have ended up with. I had to sort descending to get the latest news items.
It got more complicated in the end too ... as I had to check that the language of the news node was the same as that of the requesting site! The code to check that went in the first foreach.
Yes, you need to use a Macro Partial View rather than a Razor MacroScript. Using is Macro Partial Views is the recommended method for using Razor with WebForms templates now and performance is massively improved generally and especially for the Descendants method. Razor MacroScripts are legacy.
Select latest node from multiple sections
I have an Umbraco installation which maintains a number of websites. Each website has a news section. I need to select the top / latest 4 news items from the various websites, each website can only provide one news item, so some websites will not provide any (because they are not in the 4 most recent). What is the best way to do that with Razor?
I am using Umbraco V6 (not MVC).
So, I have something like:
website A News News A1 ... News AN website B News News B1 ... News BN etc
At the moment, I cannot guarantee that the latest news item is in any particular place in the list (i.e. first or last), but probably could do if that would help.
Hi Gordon,
I'm going to assume you can use a Partial View Macro and have Umbraco v6.1+ (if not we can come up with something different). So assuming all news articles are of the same document type in all websites you can use TypedContentAtXPath to select them all, e.g.
Jeavon
I don't think that will satisfy the "each website can only provide one news item" requirement though?
would this linq discussion on stackoverflow guide you in the right direction...
http://stackoverflow.com/questions/470440/how-to-select-only-the-records-with-the-highest-date-in-linq
Ooh yeah, how about something like this?
Is Umbraco.TypedContentAtRoot an MVC thing, as it doesn't seem to work in my Razor macroscript? (Using v6.1.6 and webforms, not MVC)
Other than that, your solution is pretty much what I have ended up with. I had to sort descending to get the latest news items.
It got more complicated in the end too ... as I had to check that the language of the news node was the same as that of the requesting site! The code to check that went in the first foreach.
Yes, you need to use a Macro Partial View rather than a Razor MacroScript. Using is Macro Partial Views is the recommended method for using Razor with WebForms templates now and performance is massively improved generally and especially for the Descendants method. Razor MacroScripts are legacy.
Glad you came up with a solution!
is working on a reply...
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.