How to: Sort search queries in SharePoint 2013

Posted Thursday, January 3, 2013 2:58 PM by CoreyRoth

Sorting search results has always been an interesting subject in SharePoint.  It was actually harder to do than you would think.  In SharePoint 2007, all you had was relevance and modified date (write).  The KeywordQuery class did not have anything to allow you to sort.  You could get around this though by using SQL Syntax though but it was not ideal.  As a reminder, you should stop using SQL Syntax now.  In SharePoint 2010, Microsoft added a SortList property to the KeywordQuery class however, if you tried to use it, you would likely get the error: Exception from HRESULT: 0x80040E60.  This is because, code was added to prevent SharePoint 2010 to sort on anything but relevance and modified date.  The only way to get sortable search results was to add FAST Search for SharePoint (FS4SP).   That was unfortunate as many people wanted to sort but did not want to make that kind of investment.

Fast forward to today, and now we can take advantage of the SortList property and it really works.  Let’s build off of my previous KeywordQuery post.  All we need to do is add items to the read-only SortList collection.  For example, to sort by author, we would add the following line before executing the query with SearchExecutor.

keywordQuery.SortList.Add("Author", SortDirection.Ascending);

Now the results are sorted by author.

SearchSortAuthorDataSetVisualizer

We could also sort in descending order as well.  For example to show files largest to smallest, use this snippet.

keywordQuery.SortList.Add("Size", SortDirection.Descending);

Here are the results:

SearchSortSizeDataSetVisualizer

These can be combined as well.

keywordQuery.SortList.Add("Author", SortDirection.Ascending);

keywordQuery.SortList.Add("Size", SortDirection.Descending);

The results then look like:

SearchSortAuthorSizeDataSetVisualizer

We can invoke this functionality when using the Search REST API as well.  To do this, we use the querytext parameter to issue our query just like before, but we add the sortlist parameter.  The syntax of the parameter is “manageproperty:sortorder”.  For sort order you must specify ascending or descending. Be sure to include the values in single quotes.  We delimit multiple sort properties using a comma (,).  Using the above sort example, here is what the query string would look like.

http://server/_api/search/query?querytext='SharePoint'&sortlist='author:ascending,size:descending'&selectproperties='Title,Author,Size,Path,Write'

From the screenshot, you can see the PowerPoint Presentation is also the top result.  I’ve also included a few selectproperties to make the results more legible.

SearchRESTAPIResultsWithSortOrder

I’m really excited that all users of SharePoint search now have this capability.  It really adds a lot of value to the solutions that we can deliver.  The last thing I will mention is that you must have the managed property you are searching on marked as sortable.  I’ll talk about new features of managed properties in a post pretty soon.

Comments

# SharePoint 2013 Nuggets of the weeks 1 & 2

Friday, January 11, 2013 2:16 AM by Ragnar Heil: SharePoint Nuggets

Now I am back at my customer`s SharePoint Project after enjoying Christmas holidays, so I want to summarize

# SharePoint 2013 Nuggets of the weeks 1 & 2 | MSDN Blogs

Friday, January 11, 2013 4:59 AM by SharePoint 2013 Nuggets of the weeks 1 & 2 | MSDN Blogs

Pingback from  SharePoint 2013 Nuggets of the weeks 1 & 2 | MSDN Blogs

# SharePoint Daily » Blog Archive » Are SharePoint Road Maps Necessary?; Microsoft Mobile Priorities; How Will Windows 8 Evolve?

Pingback from  SharePoint Daily  » Blog Archive   » Are SharePoint Road Maps Necessary?; Microsoft Mobile Priorities; How Will Windows 8 Evolve?

# Are SharePoint Road Maps Necessary?; Microsoft Mobile Priorities; How Will Windows 8 Evolve?

Friday, March 1, 2013 9:28 AM by SharePoint Daily

Today is The National Day of Unplugging . Is anyone out there to read this? - Dooley Top News Stories

# re: How to: Sort search queries in SharePoint 2013

Monday, October 21, 2013 4:28 PM by Andres Felipe

If I need to sort by result title or by my custom column ? I tried with keywordQuery.SortList.Add("Title", SortDirection.Descending); but I get an excecution error on the line : clientContext.ExecuteQuery(); , some recommendation or solution ? Thanks !

# SharePoint 2013 Nuggets of the weeks 1 & 2 | Ragnar Heil: Office 365 Nuggets

Pingback from  SharePoint 2013 Nuggets of the weeks 1 & 2 | Ragnar Heil: Office 365 Nuggets

# Are SharePoint Road Maps Necessary?; Microsoft Mobile Priorities; How Will Windows 8 Evolve? – Bamboo Solutions

Pingback from  Are SharePoint Road Maps Necessary?; Microsoft Mobile Priorities; How Will Windows 8 Evolve? – Bamboo Solutions

Leave a Comment

(required) 
(required) 
(optional)
(required)