Welcome!

Apache Authors: Kevin Benedict, Noel Wurst, David Smith, RealWire News Distribution, Max Katz

Blog Feed Post

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.

Read the original blog entry...

More Stories By Corey Roth

Corey Roth, a SharePoint Server MVP, is a consultant at Hitachi Consulting specializing in SharePoint and Office 365 for clients in the energy sector. He has more than ten years of experience delivering solutions in the energy, travel, advertising and consumer electronics verticals.

Corey specializes in delivering ECM and search solutions to clients using SharePoint. Corey has always focused on rapid adoption of new Microsoft technologies including Visual Studio 2013, Office 365, and SharePoint.

He is a member of the .NET Mafia (www.dotnetmafia.com) where he blogs about the latest technology and SharePoint. He is dedicated to the community and speaks regularly at user groups and SharePoint Saturdays.