Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Umbraco: How To Retrieve Top Recent Posts (Razor)

0.00/5 (No votes)
25 Feb 2016 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

<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.

@{
	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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here