Click here to Skip to main content
15,905,781 members
Articles / Programming Languages / C#
Tip/Trick

Umbraco: How To Retrieve Top Recent Posts (Razor)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
25 Feb 2016CPOL1 min read 8K   1  
Get the most recents posts over the whole CMS

Introduction

This script loads the top recent posts of a repository type, which is used multiple times, from any node in Umbraco.

Background

In our Umbraco CMS, we have 5 blog post repositories for now. So we want to show the three most recent posts, which have been published, in the footer.

Last three blog posts:

Last three blog posts:

Using the Code

HTML
<aside class="widget widget-posts">
	<h5 class="widget-title">Die letzten Posts</h5>
		@{
			var postRepoNodes = Model.Content.AncestorOrSelf
			("Home").Descendants("BlogPostRepository");
			
			const int topCount = 3;
			
			Dictionary<int, DateTime> postList = new Dictionary<int, DateTime>();
			
			foreach (var postRepoNode in postRepoNodes)
			{
				foreach (var post in 
				(new UmbracoHelper(UmbracoContext.Current).
				TypedContent(postRepoNode .Id))
					.Children.Where(i => i.IsVisible()).OrderBy
					("CreateDate desc").Take(topCount))
				{
					postList.Add(post.Id, post.CreateDate);
				}
			}			
			
		var posts = (from pair in postList
					 orderby pair.Value descending
					 select pair).Take(topCount);
		
		@:<ul class="recent-posts">
			foreach (KeyValuePair<int, DateTime> post in posts)
			{
				var postData = Umbraco.Content(post.Key);
				
				@:<li>
					@:<h5><a href="@postData.Url">@postData.Name</a></h5>
					@:<small><i class="fa fa-clock-o"></i> 
					@postData.CreateDate.ToString("dd.MM.yyyy")</small>
				@:</li>
			}
		@:</ul>
	}
</aside>

"Home" is the base node of our CMS, and also the starting point for searching blog repositories.

"BlogPostRepository" is the alias of the document type you want to select for searching.

The "topCount" property defines how many posts should be selected.

If you change the starting point (in this case "Home") to a child node, the script will parse only the children of this node.

Then select the top recent posts of each node of the desired document type, put them in a Dictionary and get the top posts of this dictionary to display them.

Points of Interest

A more basic approach would be to simply select all blogs and get the top posts, but the approach in this tip provides more flexibility, for example selection from different document types / child types.

HTML
@{
	var postNodes = Model.Content.AncestorOrSelf("Home").Descendants("BlogPost")
					.Where(i => i.IsVisible()).OrderBy("CreateDate desc").Take(3);
	
	@:<ul class="recent-posts">
		foreach (var post in postNodes)
		{
			@:<li>
				@:<h5><a href="@post.Url">@post.Name</a></h5>
				@:<small><i class="fa fa-clock-o"></i> 
				@post.CreateDate.ToString("dd.MM.yyyy hh:mm")</small>
			@:</li>
		}
	@:</ul>
} 

History

  • 20.02.2016 - Initial publication

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --