Wednesday 14 September 2011

CoreResultsWebPart ; Creating your own Search Results Web Part

Core Results WebPart is a famous web part that allows you to query data, filter it and display it, quite nicely. In Sharepoint 2007 we were not able to do anything with that web part, but in Sharepoint 2010 Microsoft provides us a mecahanism to create your own web parts with the CoreResultsWebPart class.

What you do, basically is override the method:

protected override void ConfigureDataSourceProperties()

From there you can collects the CoreResultsDatasource and sets the sort order you are interested.


I personally didn’t know where to start with this, as Microsoft doesn’t provide plenty of information about it, but after reading an article of Bart-Jan Hoeijmakers I understood the logic behind. I have copied/pasted his code, improved it a little bit and put it nice for you.


What you do it is create a web part, but instead of use WebPart as base class your use CoreResultsWebPart and override the main method.


The positive thing about this issue, it is the fact you can display extra properties in edit mode to refine the search even more.


I am going to do a Step by Step project and at the end of the article you will be able to find the source code to download (but read it first!).


1- Go to Visual Studio 2010->New Project->Sharepoint->2010->Empty Sharepoint Project. Call it netsourcecodeCoreResultsWebPart.


2- Go to your project-> Right Click->Add new-> Web Part. Call it CoreSearchWebPart.


3- Add the Microsoft.Office.Server.Search in the references folder.


4- Go to CoreSearchWebPart.cs, remove all the code and paste the one below.

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.Office.Server.Search.WebControls;
namespace netsourcecodeCoreResultsWebPart.CoreSearchWebPart
{
    [ToolboxItemAttribute(false)]
    public class CoreSearchWebPart : CoreResultsWebPart
    {
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [WebDescription("Sort by this managed property")]
        [Category("NetSourceCode")]
        public string OrderByProperty { get; set; }
                
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [WebDescription("Sort direction")]
        [Category("NetSourceCode")]
        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 ex)                
                {
                    throw ex;  
                }            
            }        
        }
        //protected override void CreateChildControls()
        //{
        //}
    }
}

5- Deploy the solution!


6- Go to your site, Site Actions->Edit Page->Page->Insert and go to Custom you should able to see the CoreSearchWebPart, click on Add.


image


7- Edit the Web part… this should be the result.


image


Conclusion: CoreResultsWebPart Specifies the user interface (UI) control that displays the results for a search query in the SharePoint Enterprise Search UI, with the possibility of showing anything you want.


image