Most websites will have the latest news or latest blog area. In order to build these types of pages, you'll need to query Umbraco in order to get a list of specific published pages in date order. Surprisingly, getting a list of pages sorted in date order isn't as intuitive as you might expect, so in today's post, I'm going to cover everything you need to know in order to get you up and running.
As I've mentioned many times, when you work with Umbraco you have two options for getting pages out of Umbraco. The first option doesn't use any caching and talks to the database directly. To get a list of pages on your website, I wouldn't recommend using this because it will make your web pages load slowly. Instead, I'm going to assume you'll be using the API that uses the Umbraco cache, or, in other words, uses the Lucene index, so we'll be working with IPublishedContent and the ExamineManager.
When you work with your document types with examining, then out of the box, your dates won't be searchable. This is very annoying and a little frustrating. Personally, I spent a good few hours scratching my head trying to figure out why my posts weren't ordering correctly.
If you want to sort by a custom date based on one of your document types, then you'll need to create a custom examine index and then add your date field into make that field sortable. I won't cover how to create a custom index here, if you want to know how to create one, I suggest you read, How To Set Up A Custom Lucene Index With Umbraco. However, I will share with you the XML for defining an index.
<add Name="id" EnableSorting="true" Type="Number" />
<add Name="nodeName" EnableSorting="true" />
<add Name="updateDate" EnableSorting="true" Type="DateTime" />
<add Name="writerName" />
<add Name="path" />
<add Name="nodeTypeAlias" />
<add Name="parentID" />
<add Name="postDate" EnableSorting="true"/>
<add Name="blogPage" />
The important things to note here is that my custom date 'postDate', is defined in the 'IndexUserFields' and it has the EnableSorting=true. Another confusing point that I should clarify, is that you don't need to define it as a date as the fields in the index attribute fields do. If you add the type to the field definition, your search, oddly, won't filter correctly. I'm not sure if this is a bug, or by default, but it's definitely confusing! If you look at the IndexAttributeFields fields, you'll see that the default fields have sortable and the type defined. However, from my tests adding DateTime to a custom date, does nothing, except breaking your OrderBy() so don't add it.With your index defined, with your sortable date property., the next step is to write some code to query Umbraco. The code you'll need to sort your posts by your custom date is:
var umbracoHelper = new UmbracoHelper();var blogs= ExamineManager.Instance.SearchProviderCollection["Blogs"].CreateSearchCriteria().NodeTypeAlias("blogPage")var results = umbracoHelper.TypedSearch(tutorials).OrderByDescending(x => x["postDate"]).Take(take.Value);
Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge