| By Corey Roth | Article Rating: |
|
| January 3, 2013 03:58 PM EST | Reads: |
950 |
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.
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:
These can be combined as well.
keywordQuery.SortList.Add("Author", SortDirection.Ascending);
keywordQuery.SortList.Add("Size", SortDirection.Descending);
The results then look like:
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.
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.
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...
Published January 3, 2013 Reads 950
Copyright © 2013 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Corey Roth
Corey Roth, a SharePoint Server MVP, is a consultant at Infusion specializing in SharePoint 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 2010, .NET Framework 4.0, and Silverlight.
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.
- Cloud People: A Who's Who of Cloud Computing
- Windows Azure IaaS Reaches General Availability
- Portable Experimenter’s Platform, Powered by Raspberry Pi
- Predixion Software Announces General Availability of the Latest Version of its Predictive Analytics Platform
- Cloud Expo New York: The Big Challenge of Big Data & Hadoop Integration
- Agile Solutions for Cloud, Big Data, Mobility Services
- MicroStrategy Announces General Availability of MicroStrategy 9.3.1
- Cloud Computing: Cutting Costs, Boosting Profits
- AMAX Launches StorMax(TM) CFS, powered by IBM(R) General Parallel File System(TM) (GPFS(TM))
- Big Data: Visualizing the Strategic Business Imperative
- Benefits of Cloud Computing
- MicroStrategy Announces General Availability of MicroStrategy 9.3.1
- Cloud People: A Who's Who of Cloud Computing
- Windows Azure IaaS Reaches General Availability
- Portable Experimenter’s Platform, Powered by Raspberry Pi
- Predixion Software Announces General Availability of the Latest Version of its Predictive Analytics Platform
- SUSE Receives Common Criteria Security Certifications
- Basho Announces Open Source Riak CS and General Availability of Riak CS Enterprise v1.3
- Cloud Expo New York: Big Time - Introducing Hadoop on Azure
- Cloud Expo New York: Real-Time Analytics Using an In-Memory Data Grid
- Cloud Expo New York: The Big Challenge of Big Data & Hadoop Integration
- Help Desk Solution Empowers Employees
- Red Hat Unveils Big Data and Open Hybrid Cloud Direction
- Public Cloud’s Got a Silver Lining: Gartner
- The Top 250 Players in the Cloud Computing Ecosystem
- Web Services Using ColdFusion and Apache CXF
- Cloud People: A Who's Who of Cloud Computing
- Red Hat Named "Platinum Sponsor" of Virtualization Conference & Expo
- Cloud Expo New York Call for Papers Now Open
- Eclipse "Pollinate" Project to Integrate with Apache Beehive
- An Introduction to Ant
- Cloud Expo 2011 East To Attract 10,000 Delegates and 200 Exhibitors
- Beehive Code Now Available in Apache
- Apache's Tomcat 5.5 is First Release Ever to Use Eclipse JDT Java Compiler
- 4th International Cloud Computing Conference & Expo Starts Today
- "Beehive" Now Officially an Open Source Project: Apache Beehive



















