Extending CoreResultsWebPart in SharePoint 2010 to Show Search Result in a Custom Data Control - Netwoven

Extending CoreResultsWebPart in SharePoint 2010 to Show Search Result in a Custom Data Control

By Soma Choudhuri  •  July 24, 2013  •  2622 Views

Netwoven Blog

You have SharePoint 2010 deployed. You want to display your search results in a data grid format. Data Grid display would then give you opportunity to sort, filter and page.

Solution

The OOTB SharePoint 2010 CoreResultsWebpart provides the placeholder to display the search results, but by extending this webpart by adding the DataGrid control and custom loading the search result, we can get the grid visualization.

Coding Example

Let’s fire-up the Visual Studio in your SharePoint Development Server and create a visual webpart project.

This can be done with the following steps:

1. Add reference to Microsoft.Office.Server.Search.dll from

Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14ISAPI

2. In webpart.cs file Inherit the webpart class from CoreResultsWebPart instead of default webpart. Then add the namespace Microsoft.Office.Server.Search.WebControls. The webpart is now inherited from CoreResultsWebPart.

3. Now, go to the usercontrol page (.ascx) of the visual webpart and add a gridview by dragging it from the Visual Studio toolbox.

4. In codebehind of the usercontrol (.ascx.cs) add a method named LoadSearchGrid and add the following namespaces:

using Microsoft.Office.Server.Search;
using Microsoft.Office.Server.Search.Query;
using Microsoft.Office.Server.Search.WebControls;

QueryManager object is used to get the xml returned by SharePoint Search and added as a datasource (xml Data Source) to the gridview. To achieve this, add the following codes to LoadSearchGrid method:

// gets the instance of the querymanager
QueryManager queryManager = SharedQueryManager.GetInstance(this.Page).QueryManager;

Thereafter, define a DataSet and DataTable object.

DataSet dtSet = null;

DataTable dtTable = null;

To get the Xml returned by Search add the following codes:
if (queryManager != null && queryManager.Count > 0)
{
XmlDocument xdoc = new XmlDocument(); //We are using XmlDocument
xdoc = queryManager.GetResults(queryManager[0])//xml returned by search

// Get blended results
if (xdoc != null)
{
XmlReader xmlReader = new XmlNodeReader(xdoc);
dtSet = new DataSet();
dtSet.ReadXml(xmlReader);

if (dtSet.Tables.Count > 1)
{
dtTable = dtSet.Tables["Result"];

//Add the DataTable object as a dataSource to GridView added in ascx page by the following codes:

gridView.DataSource = dtTable;
gridView.DataBind();

}

}

}

Finally, Call the LoadSearchGrid function from Page_Load method by adding the following block of codes:

if (!Page.IsPostBack)
{
LoadSearchGrid();
}

5. Handle the exception using proper try catch blog in code.

6. Build the solution and deploy. The search will be shown in gridview now.

The query, fetched properties and every configurable properties of the CoreResultsWebpart in the custom CoreResultsWebpart can be manipulated.

For sorting search result in gridview, a custom method is added which shall be discussed in my next article.

Hope you will find this blog helpful and look forward to your comments!

Soma Choudhuri

Soma Choudhuri

Soma Choudhuri is the Sr. Technical Architect at Netwoven Inc. focused on Enterprise Content Management (ECM). In her 19 years of career, her primary area of expertise has been developing and implementing SharePoint collaboration portals and .NET web applications. Soma has been a leading Microsoft technologies expert for clients across USA and India. Prior to joining Netwoven, she held many organizational leadership roles across manufacturing and financial services industries.

6 comments

  1. Just modify the xslt on the existing webpart to show items in a grid. you could do sorting, filtering and paging that way.

    1. When modifying the search core results web part via xslt & showing in a grid, only the visible page of results is sorted, filtered, grouped. So if your search result set , a custom xslt will not give the desired results.

      1. Sorry, meant to say that if your search results is over 50 items a custom xslt will not give the desired results. Only the visible results are filtered, etc. So if your result set was several pages you will need a custom core results web part.

  2. Its a good practice to modify the search result webpart xslt to display the search result in a table like fashion. That way the paging, sorting would be easy and everything is out of the box as well.

    But here we are not doing that, we are binding the search result to a custom data control (grid view, datalist etc.) for our requirement and for that we are only considering the xml data source of the search result web part. We had a different requirement that was beyond the customization of xslt.

  3. Thanks Jayanta, this will help me and my team a great deal as we are new to this technology. We appreciate you putting together this and look forward to more of your guides if possible.

    John.

Leave a comment

Your email address will not be published. Required fields are marked *

Dublin Chamber of Commerce
Microsoft Partner
Microsoft Partner
Microsoft Partner
Microsoft Partner
Microsoft Partner
Microsoft Partner
Microsoft Fast Track
Microsoft Partner
Microsoft Fabric
MISA
MISA
Unravel The Complex
Stay Connected

Subscribe and receive the latest insights

Netwoven Inc. - Microsoft Solutions Partner

Get involved by tagging Netwoven experiences using our official hashtag #UnravelTheComplex