Recent post list from multiple parents

  • Tuesday, June 17, 2014 10:30 PM EST
  • Last Edited: Saturday, June 21, 2014 4:56 PM EST

How to create a list of recent posts from multiple sources (like this blog where I pull from 'Stories' and 'Learn') ordering them by publish date and then by create date in umbraco 7 partial view. The following steps were done to achieve this:

  1. Pull most recent posts of the parent pages
  2. Create a variable to merge the two (or more) list together. Create another variable that will hold the final list in the right order
  3. Loop through each parent then add them to temporary list collection. Remember, Umbraco adds them as DynamicPublishedContent
  4. Sort the temporary list using LINQ syntax by the date then store again in final list.
  5. Loop through the final list to display every page

@inherits UmbracoTemplatePage

@{

    var homePage =  CurrentPage.AncestorsOrSelf(1).First();

   

    // Get parents

    var codingOverview =  Umbraco.Content(homePage.codingparent);

    var blogOverview = Umbraco.Content(homePage.blogparent);

 

    //get items orderby publish date then create date

    var codingItems =  codingOverview.umbNewsItems.OrderBy("publishDate  desc, createDate desc").Take(4);

    var blogItems = blogOverview.umbNewsItems.OrderBy("publishDate desc, createDate desc").Take(4);

 

    //create a temporary list to store all items then a final list to sort again

    List<Umbraco.Web.Models.DynamicPublishedContent>  tempList = new List<Umbraco.Web.Models.DynamicPublishedContent>();

    List<Umbraco.Web.Models.DynamicPublishedContent>  finList = new List<Umbraco.Web.Models.DynamicPublishedContent>();

 

    // Merge two lists

    foreach(var item in codingItems){

       tempList.Add(item);

    }

      

    foreach(var item in blogItems){

       tempList.Add(item);

    }

      

     finList  = tempList.OrderByDescending(item=> item.UpdateDate).ThenByDescending(item=>  item.CreateDate).ToList();

}

 

<h2 class="major"><span>Recent Posts</span></h2>

 

<!-- Archives -->

<ul>

    @foreach (var thePost in finList)

    {

        // Get page title

        var title = thePost.GetProperty("Title");

             

        // If the editor has not explicitly set the publishDate  property then show the create date

        var dateTime = thePost.UpdateDate == default(DateTime)

            ?  thePost.CreateDate

            :  thePost.UpdateDate;

 

        <li>

            <article class="is-post-summary">

                <h3><a href="@thePost.Url">@title</a></h3>

                <ul class="meta">

                    <li class="timestamp">@dateTime.ToString("f")</li>

                </ul>

            </article>

        </li>

    }

</ul>

<!-- /Archives -->

 

 

comments powered by Disqus