Copied to clipboard

Flag this post as spam?

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


  • Nick 37 posts 130 karma points
    Aug 11, 2016 @ 15:22
    Nick
    0

    Get latest media items from entire media tree

    Hi,

    I need to get a list of the latest 10 items added to the entire media tree, not from a specific folder.

    I want to do this in a partial view macro using razor.

    Is there a quick way to do this?

    thanks,

    nick

  • Søren Kottal 706 posts 4503 karma points MVP 6x c-trib
    Aug 11, 2016 @ 20:21
    Søren Kottal
    100

    You can do it with Examine

    @{
        var searcher = ExamineManager.Instance.SearchProviderCollection["InternalSearcher"];
        var searchCriteria = searcher.CreateSearchCriteria();
        var results = searcher.Search(searchCriteria.RawQuery("nodeTypeAlias:Image"));
        if (results != null && results.Any())
        {
            var resultsOrdered = results.OrderByDescending(x => DateTime.ParseExact(x.Fields["createDate"], "yyyyMMddHHmmssfff", System.Globalization.CultureInfo.CurrentCulture));
            var resultsTop10 = resultsOrdered.Take(10);
    
            foreach (var result in resultsTop10)
            {
                @result.Fields["nodeName"]
            }
        }
    }
    
  • Nick 37 posts 130 karma points
    Aug 12, 2016 @ 08:07
    Nick
    0

    Hi Soren,

    That's great. Solution is working nicely. Thanks for your help.

    I amended slightly to get the URL of the File items:

    var results = searcher.Search(searchCriteria.RawQuery("nodeTypeAlias:File"));
                        if (results != null && results.Any())
                        {
                            var resultsOrdered = results.OrderByDescending(x => DateTime.ParseExact(x.Fields["updateDate"], "yyyyMMddHHmmssfff", System.Globalization.CultureInfo.CurrentCulture));
                            var resultsTop5 = resultsOrdered.Take(5);
                            foreach (var result in resultsTop5)
                            {
                                var med = Umbraco.Media(result.Fields["id"].ToString());
                                string url = @med.umbracoFile;
                                //var url = result.Fields["urlName"].ToString();
                                <li><a href="@url">@result.Fields["nodeName"]</a></li>
                            }
                        }
    

    cheers,

    Nick

Please Sign in or register to post replies

Write your reply to:

Draft