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 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.

Comments

 

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

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

January 10, 2013 7:52 PM
 

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

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

February 26, 2013 7:57 AM
 

SharePoint Daily said:

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

February 26, 2013 8:08 AM
 

Linkapalooza: January 16, 2013 « SDTimes said:

Pingback from  Linkapalooza: January 16, 2013 « SDTimes

April 24, 2014 1:14 PM
 

Mark said:

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?

June 16, 2014 10:45 AM
 

CoreyRoth said:

@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.

June 16, 2014 10:54 AM

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