My Story
By default, it’s only possible to sort on relevance and modified date in the Core Results Web Part. And it also isn’t possible to give a sorting direction, it’s always descending.
Now a client wanted to show the last created discussions on the homepage of it’s SharePoint, off course sorted by creation date. Out of the box this isn’t possible, but luckily somebody else had the same problem and came with a neat solution: http://www.insidesharepoint.net/post/2011/02/16/Creating-a-sortable-CoreResultsWebPart-in-SharePoint-2010.aspx
I played around a bit with that code, but couldn’t get it to work. The most common error I got was Property doesn’t exist or is used in a manner inconsistent with schema settings.
At this time of writing my expertise in the SharePoint Search maze isn’t that great, but I read something about a Managed Metadata Properties in the blog post. After playing around with custom managed properties for a while I finally got it to work. The trick is to create a custom Managed Property with mappings to, in my case, Basic:15;ows_start_x0020_Date,Office:12;creationdate and do a full crawl. Important! Do not do an incremental crawl! That will definitely not work! I cracked my head on that one already!
And then it works!
You could also forget the story above and follow the instructions beneath
These are the steps I took to work out my case, which is sorting Team Discussions by Creation Date.
First I created a scope named Discussions with a rule contentclass = STS_ListItem_DiscussionBoard.
Then I created a Managed Metadata Property named Created with a few mappings and settings like below.
Next thing to do is to start a full crawl. No incremental, that won’t work!
Now we can create our own Web Part. Create a new Visual Studio 2010 project, call it SortableCoreResultsWebPart. Add a reference to Microsoft.Office.Server.Search and add a new Web Part to the project, no Visual Web Part, just a regular. Name it SortableCoreResultsWebPart.
Open the SortableCoreResults.cs and overwrite the code with the following code.
public class SortableCoreResultsWebPart : CoreResultsWebPart { [Personalizable(PersonalizationScope.Shared)] [WebBrowsable(true)] [WebDescription("Sort by this managed property")] [Category("MyProperties")] public string OrderByProperty { get; set; } [Personalizable(PersonalizationScope.Shared)] [WebBrowsable(true)] [WebDescription("Sort direction")] [Category("MyProperties")] public Microsoft.Office.Server.Search.Query.SortDirection SortDirection { get; set; } /// <summary> /// Override. First runs the base code, than collects the CoreResultsDatasource and /// sets the SortOrder property. /// </summary> protected override void ConfigureDataSourceProperties() { // only do stuff when search results are visible if (this.ShowSearchResults) { // run the base code base.ConfigureDataSourceProperties(); try { // if OrderByProperty is not set, use default behaviour if (!string.IsNullOrEmpty(OrderByProperty)) { // get the datasource and change the sortorder CoreResultsDatasource dataSource = this.DataSource as CoreResultsDatasource; dataSource.SortOrder.Clear(); dataSource.SortOrder.Add(OrderByProperty, SortDirection); } } catch (Exception) { // handle this error! and show a friendly error message } } } }
Don’t forget to add an using for Microsoft.Office.Server.Search.WebControls.
Run the thing and add the Web Part to the page. An error will pop up saying SrhdcNoKeywordException was unhandled, skip it by pressing F5 or clicking continue a couple of times.
Edit the Web Part, open the Location Properties and modify the scope to Discussions. Open Result Query Options and modify Fixed Keyword Query to contentclass:STS_ListItem_DiscussionBoard. Finally fill the OrderByProperty with Created and set a SortDirection.
Save and refresh the page. Watch your web part sort like Dustin Hoffman in Rain Man!
Thanks very much!! Excellent post… +1.
Thanks for the post, its useful, i used ur idea and created a dicussion rollup webpart by extending core search results.i added to my homepage. do i need to do full crawl everytime to get new updated results by creationdate ? after full crawl, i added a discuusion item and then ran incremental crawl, results sorting didnt work. let me know your thoughts
You’re welcome. The full crawl is only initial. After a full crawl you can do incremental crawls. If that doesn’t work for you it would likely be a problem with the search configuration.
Thanks Jeff, my requirement is little different , my user wants the results to be displayed by last updated( discussion item last commented ) so i created a new managed property LastModified and mapped to ows_DiscussionLastUpdated. and applied sorting on the managed property… my results are sorted and getting updated only on full crawl, but not on incremental crawl…
when i look for tht new comment text in sharepoint default search, i can find in the search results after incremental crawl.so i think my search is configured correct.
Hey Jeff Disregard my previous comment,i think time is the problem
is there anyway to get lastupdated column value as date and time
my lastupdated managed properties is giving only date instead of date and time and i was sorting on lastupdated
Pingback: SharePoint 2010 People Directory Part 3 - Sorting at SharePoint Config
You can save sometime if you update the webpart properties directly, that way you won’t need a deploy or any downtime in your sistem.
See the instructions on this blog: http://blog.peanuts.no/index.php/2013/07/customsortingincoreresultswebpart/
Thanks for the tutorial, that indeed is a good alternative. Though I believe my option is more permanent and stable, but your option is quicker to realize.