In today's post, you will learn how to sort items returned by the Umbraco Examine index. 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 I wrote this tutorial to help you go through the same pains that I did 😊😊😊

How To Get The Pages From Umbraco

As I've mentioned many times, when you work with Umbraco you have a few options for getting page data out of Umbraco. One option doesn't use any caching and involves talks to the database directly using ApplicationContext. I wouldn't recommend using this for read-only calls as it's super slow. Umbraco does have a page cache, however, this cache works best for individual page lookups. This cache is not as optimal when you need to query for a lot of pages at the same time. Instead, I'm going to assume you'll be using the API that uses the Umbraco index, or, in other words, the Lucene index. Following this approach, we will be working with IPublishedContent and the ExamineManager.

Lucene Indexes

When you work with your document types with the ExamineManager`, out-of-the-box, the published date will not be searchable. This is very annoying and a little frustrating. The reason for this is that pages in the examine cache are treated as just content and not published content. Personally, I spent a good few hours scratching my head trying to figure out why my posts weren't ordering correctly.

If you scheduled content, you will not have access to a standard publish date property. To render these pages in specific date order, you will want to add a custom date field to your document type. Adding a custom date make things interesting. The default Examine index contains everything. All document types, meaning all pages on your site. If you want a page to only render blogs, or, news articles, these document types are the only ones that you will want to add this custom date field on. This means you can not cleanly write some code using the default index.

To create a page that only returns document types where you have added this custom date field, you are better off creating a custom examine index that includes this data and the date field. Doing this will allow you to make the date 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. I will however include the XML for required for defining an index:

The important thing to note here is that my custom date postDate, is defined in the IndexUserFields and it has the property EnableSorting set to true. 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, the next step is to write some code to query Umbraco. The code will need to sort your posts by this custom date property, the code to do this looks like this:

With a custom index, and some custom code you can sort to your heart's desire is meet! Happy Coding 🤘