| By Corey Roth | Article Rating: |
|
| January 3, 2013 03:58 PM EST | Reads: |
970 |
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 970
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
- AMD and Adobe Collaborate on Upcoming Version of Adobe Premiere Pro Software to Enable Breakthrough Video Editing Performance Through Open Standards
- State and Local Governments Adopt Microsoft Dynamics CRM to Improve Citizen Service Delivery
- New Relic Q1 2013 Blazes Past Growth Targets and Reaches 40,000 Active Customer Accounts
- Predixion Software Announces General Availability of the Latest Version of its Predictive Analytics Platform
- Cloud Expo New York: Deploying Hybrid Cloud for Performance and Uptime
- CollabNet And UC4 Announce General Availability Of Joint Enterprise DevOps Platform
- Symphony EYC Appoints New Account Manager to Drive Global Opportunities
- Cloud Computing Is Simplifying Things
- Cloud Expo New York: Developing the World’s First IaaS Marketplace
- Session Topics: 12th Cloud Expo / Cloud Expo New York
- Cloud People: A Who's Who of Cloud Computing
- Cloud Expo New York: Best CIO Practices Shared from SHI’s Customers
- Windows Azure IaaS Reaches General Availability
- AMD and Adobe Collaborate on Upcoming Version of Adobe Premiere Pro Software to Enable Breakthrough Video Editing Performance Through Open Standards
- State and Local Governments Adopt Microsoft Dynamics CRM to Improve Citizen Service Delivery
- New Relic Q1 2013 Blazes Past Growth Targets and Reaches 40,000 Active Customer Accounts
- The PostOpen Event – Why It Is So Important
- The Cover and the Epilogue of the Upcoming Book
- Predixion Software Announces General Availability of the Latest Version of its Predictive Analytics Platform
- Cloud Expo New York: Deploying Hybrid Cloud for Performance and Uptime
- Small Cancers, Big Data, and a Life Examined
- Global Micro Servers Market (2013 - 2018), By Processor Type (Intel, Arm, Amd), Component (Hardware, Software, Operating System), Application (Media Storage, Data Centers, Analytics, Cloud Computing) & Geography (North America, Europe, Apac, Row)
- Google Maps and ASP.NET
- Converting VB6 to VB.NET, Part I
- How to Write High-Performance C# Code
- Crystal Reports XI & How It Has Changed
- Where Are RIA Technologies Headed in 2008?
- Creating Controls for.NET Compact Framework in Visual Studio 2005
- Programmatically Posting Data to ASP .NET Web Applications
- Implementing Tab Navigation with ASP.NET 2.0
- AJAX World RIA Conference & Expo Kicks Off in New York City
- i-Technology Viewpoint: "SOA Sucks"
- .NET Archives: Getting Reacquainted with the Father of C#
- i-Technology Photo Exclusive: Bill Gates & Steve Jobs In "Nerds"
























