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:

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
        [WebDescription("Sort by this managed property")]
        public string OrderByProperty { get; set; }

        [WebDescription("Sort direction")]
        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
                    // 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.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!

People get lazier every day. I guess it’s some kind of evolution. A few thousand years ago we used to make our own tools. We had to go out into the woods to hunt for a meal,  and with a bit of luck we had a piece of meat at night. Nowadays we drive to work, bang on the keyboard for 8 hours, drive back, and at home a not too scanty meal magically appears on the table. Did we hunt for that? Did we make our own car? Did we make our own PC? Did we even make our own OS? We didn’t even sweat! In the history of mankind someone apparently was smart enough to not kill the caught animal, but let it jump on top of a female of the same kind, and then wait for the beef to spawn on his plate. People get smarter, so they can be lazier. Evolution.

Now back to SharePoint. SharePoint is the perfect platform for the current generation of people. We can find all of our documents, tasks and emails in one place. People even expect one overview for content spread across multiple sites. To achieve this kind of content aggregation, SharePoint 2010 has a few tools available.

In the latest SharePoint eMagazine of the DIWUG (Dutch Information Workers User Group) Brett Lonsdale writes about the multiple tools SharePoint has, that can achieve content aggregation over multiple lists, sites, site collections and even farms. I would really recommend reading the article, but for the lazy (or more evolved) readers, here is a summary.

  • Content by Query Web Part – This is part of the Publishing Infrastructure Feature. It allows you to aggregate content from lists of the entire site collection. This can be done by simple clicking and selecting. This also applies to filtering, grouping and sorting. Out of the box there are multiple styles available, but you can also create your own style by copy and modify a XSL in the Style Library.
    The CQWP is a very powerful tool for content in one site collection. It won’t work with multiple site-collections.
  • Relevant Documents Web Part – This is a very simple web part which doesn’t allow much configuring. You can show your last modified documents, or the documents you created, but it doesn’t get more exciting than that. Oh, and you have to keep in mind that it’s only for the current site, not for the site collection.
  • RSS Feed Web Part – The RSS Feed web part can show the RSS feed of a SharePoint List. If you are using Kerberos you can get the RSS feed of a list in the web part. This is useful across site collections.
  • Data View Web Part – This web part can be found in every SharePoint 2010 version, even foundation. The RSS feed can also be used in this web part. Even more, multiple RSS feeds can be merged, talking about content aggregation. In SharePoint designer there is even more available with this web part. Data sources can be web services, databases, xml, etc.
  • Core Search Results Web Part – This web part is useful when you want to aggregate content of multiple site collections. This can be done by configure a search scope with properties and rules in central admin. One big disadvantage is that the content is as up to date as the latest crawl.
  • Calendar Overlays – In a SharePoint calendar you can get an overlay using the Calendar Overlays button on the ribbon. This function is pretty straightforward.
The Content Query Web Part is a very powerful OOTB tool for content aggregation. If you want even more then use the Data View Web Part in combination with SharePoint Designer.