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

Posted Wednesday, January 9, 2013 2:21 PM by CoreyRoth

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.

Comments

# My SharePoint links January 11, 2013 | Jeremy Thake's musings

Pingback from  My SharePoint links January 11, 2013 | Jeremy Thake's musings

# 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 » SharePoint 2013 Social Features; Microsoft Confused by Microsoft Software; The Red Cross Goes to the Cloud

Pingback from  SharePoint Daily  » Blog Archive   » SharePoint 2013 Social Features; Microsoft Confused by Microsoft Software; The Red Cross Goes to the Cloud

# SharePoint 2013 Social Features; Microsoft Confused by Microsoft Software; The Red Cross Goes to the Cloud

Tuesday, February 26, 2013 8:08 AM by SharePoint Daily

While April may not be on your radar, is it ever too early to save money? Register now to save $300 off

# Linkapalooza: January 16, 2013 « SDTimes

Thursday, April 24, 2014 1:14 PM by Linkapalooza: January 16, 2013 « SDTimes

Pingback from  Linkapalooza: January 16, 2013 « SDTimes

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

Monday, June 16, 2014 10:45 AM by Mark

Can I use querystring to search for a particular managed property? e.g. I have managed property "ArticleStartDate". Is it possible to search with querystring?

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

Monday, June 16, 2014 10:54 AM by CoreyRoth

@Mark yes.  Just do something such as k=ArticleStartDate>='6/1/2014'.  You can URL encode it if you prefer but it will work.  Just type it in the browser.

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

Thursday, September 11, 2014 9:32 AM by Craig

what else can we pass in using the default parameter? can we pass in something for the displaytemplate?

where did you find out about the default?

Thanks.

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

Tuesday, September 23, 2014 5:17 AM by Lars

Very informative article! Do you know if it is possible to restrict the "scope" of the query to a specific Site Collection (e.g. the "u" parameter in the classical approach). When I test this approach with the osssearchresults.aspx of my Site Collection, it works but it returns results from all Site Collections.

# results ordering by the query string | Zick Answers

Wednesday, December 10, 2014 7:56 AM by results ordering by the query string | Zick Answers

Pingback from  results ordering by the query string | Zick Answers

# 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

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

Wednesday, March 22, 2017 12:21 PM by John

Hi Corey.

That's great stuff. But how do you now all of this? I'm looking for documentation and your blog post seems to be the only information in the entire internet about that stuff.

Leave a Comment

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