Welcome!

Apache Authors: Pat Romanski, Jnan Dash, Lacey Thoms, Sandi Mappic, Michael Bushong

Blog Feed Post

How to: Sort search results by query string with the ResultScriptWebPart in SharePoint 2013

By now you may have noticed that Search has changed quite a bit in SharePoint 2013. The ResultScriptWebPart powers most of the magic behind the Search Center.  It replaces the CoreResultsWebPart that we have been using since SharePoint 2007.  I expected many things in it to operate the same, but this web part really has been rewritten from the ground up and that’s a good thing.  One new feature (of many) it brings us is the ability to sort search results.  You can specify a sort order in the query or by enabling it in the web part properties.  This allows the user to pick from a list of properties.  I’ll cover it in the future because it deserves a quick post.  However, for you developers out there, I know that some of you will want to write some code that passes a sort order.  This post explains how to do it.

Before we begin, you have to know what you can sort of.  There are a lot of options out-of-the-box such as Author and Write (Modified Date) or you can use your own property.  Whatever you choose, you need to check the managed properties page and make sure it has the Sortable option set to Yes - active.  You do this from the Search Schema page on your site collection or Search Service Application.

SearchManagedPropertySortable

Since SharePoint 2007, we have been able to pass a query using KQL using the “k” parameter to the results.aspx of your search center.  We can still do this in SharePoint 2013.  To add a sort order though, we do things a little differently.  Using a new query string parameter called Default, we can pass in a number of properties including sort order.  The format is quite a bit different though as it uses a JSON notation.  To start, I am going to issue a query with out k parameter using this new JSON notation.  Let’s do a search for our sales documents.  To do this, I include my search center URL (/search/pages/results.aspx) and append #Default=  to it followed by the JSON object with a name value pair {"k":"Sales"}.  When I assemble to entire string, it looks something like this.

http://server/search/Pages/results.aspx#Default={"k":"Sales"}

SearchResultsSales

Now we want to add sorting.  To do that, we use the “o” parameter.  It takes a collection of JSON objects.  The parameter p represents the managed property to sort by and the value d is the sort direction specified as a 0 or 1.  A value of 0 represents ascending and a value of 1 represents descending.  For example to sort by modified date descending, we use the Write managed property.  The string would look like this:

"o":[{"d":1,"p":"Write"}]

When we add it to our query string from above it looks like this. 

http://server/search/Pages/results.aspx#Default={"k":"Sales","o":[{"d":1,"p":"Write"}]}

SearchResultsSalesWriteDescending

This shows us the newest documents.  In SharePoint 2013, they moved the modified date to the InfoCard so you actually have to hover over a search result to see the date.  To show the oldest documents, you would just specify d:'”0” instead. 

As another example, we could also sort by Author.

http://server/search/Pages/results.aspx#Default={"k":"Sales","o":[{"d":0,"p":"Author"}]}

SearchResultsSalesOrderByAuthorWithInfoCard

I’ve included a bigger screenshot where you can see the name of the first author in the list on the InfoCard.

We can also combine them to have multiple sort orders, but I have had mixed results with that so far.  Here’s what the query string would an example look like though.

http://server/search/Pages/results.aspx#Default={"k":"Sales","o":[{"d":0,"p":"Write"},{"d":1,"p":"Author"}]}

I’m pretty excited about sorting functionality in the search center.  Users have been wanting it for quite a long time and only a select few ever had it (those with FAST Search for SharePoint).  If you are writing search solutions, be sure and check my post about sorting with the REST API.  This technique works with both SharePoint 2013 on-premises as well as SharePoint Online Preview.  All of the screenshots came from Office 365.

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.