Copied to clipboard

Flag this post as spam?

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


  • Gordon Saxby 1459 posts 1880 karma points
    Dec 09, 2013 @ 12:07
    Gordon Saxby
    0

    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.

  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    Dec 09, 2013 @ 20:56
    Jeavon Leopold
    0

    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.

    var allNewsItems = Umbraco.TypedContentAtXPath("//NewsItemsDocTypAlais").OrderBy(x => x.GetPropertyValue<Date>("aliasOfYourDateToSortBy")).Take(4);
    

    Jeavon

  • Gordon Saxby 1459 posts 1880 karma points
    Dec 10, 2013 @ 01:48
    Gordon Saxby
    0

    I don't think that will satisfy the "each website can only provide one news item" requirement though?

  • Mike Chambers 636 posts 1253 karma points c-trib
    Dec 10, 2013 @ 10:16
    Mike Chambers
    0

    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

  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    Dec 10, 2013 @ 11:48
    Jeavon Leopold
    100

    Ooh yeah, how about something like this?

    var myNewsCollection = Enumerable.Empty<IPublishedContent>();    
    foreach (var website in Umbraco.TypedContentAtRoot())
    {
        var siteNewsItems = website.Descendants("NewsItemsDocTypAlias");
        if (siteNewsItems.Any())
        {
            myNewsCollection.Union(siteNewsItems.Take(1));
        }
    }
    
    myNewsCollection = myNewsCollection.OrderBy(x => x.GetPropertyValue<Date>("aliasOfYourDateToSortBy")).Take(4);
    foreach (var item in myNewsCollection)
    {
        <p>@item.Name</p>
    }
    
  • Gordon Saxby 1459 posts 1880 karma points
    Dec 10, 2013 @ 16:43
    Gordon Saxby
    0

    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.

  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    Dec 10, 2013 @ 16:49
    Jeavon Leopold
    0

    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!

Please Sign in or register to post replies

Write your reply to:

Draft