in

Dot Net Mafia

Group site for developer blogs dealing with (usually) .NET, SharePoint 2013, SharePoint 2010, Office 365, SharePoint Online, and other Microsoft products, as well as some discussion of general programming related concepts.

This Blog

Syndication

Archives

Corey Roth [MVP]

A SharePoint MVP bringing you the latest time saving tips for SharePoint 2013, Office 365 / SharePoint Online and Visual Studio 2013.

How to: Sort search queries in SharePoint 2013

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

 

Ragnar Heil: SharePoint Nuggets said:

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

January 11, 2013 2:16 AM
 

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

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

January 11, 2013 4:59 AM
 

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

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

March 1, 2013 9:19 AM
 

SharePoint Daily said:

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

March 1, 2013 9:28 AM
 

Andres Felipe said:

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 !

October 21, 2013 4:28 PM

Leave a Comment

(required)  
(optional)
(required)  
Add

About CoreyRoth

Corey Roth is an independent SharePoint consultant specializing in ECM, Apps, and Search.
2012 dotnetmafia.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems