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.
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.
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:
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)//xml returned by search
// Get blended results
if (xdoc != null)
XmlReader xmlReader = new XmlNodeReader(xdoc);
dtSet = new DataSet();
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;
Finally, Call the LoadSearchGrid function from Page_Load method by adding the following block of codes:
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!