Export (0) Print
Expand All
Expand Minimize

Displaying Search Results in a Grid View in SharePoint Server 2007

SharePoint 2007

Summary:  Learn how you can bind search engine results to an SPGridView control to display the results in a grid format in Microsoft Office SharePoint Server 2007.

Office Visual How To

Applies to:  2007 Microsoft Office System, Microsoft Office SharePoint Server 2007

Microsoft Corporation

March 2008

Overview

Microsoft Office SharePoint Server 2007 provides various ways to display search results. This Microsoft Office Visual How To shows how you can bind the results from the search engine to an SPGridView control to display the results in a grid format. Using a grid to display the search results makes it much easier to compare results and their metadata. You can extend this example to provide additional functionality, for example, to perform bulk actions on the search results.

Code It

This example uses a Web Part to execute a query to the search engine and display the results. The query retrieves the task list item for all sites that are assigned to the current user.

First, create a new Microsoft Visual Studio 2005 project by using the Web Part template from the Visual Studio Extensions for Windows SharePoint Services. Make a reference in the Web Part to the Microsoft.Office.Server.Search.Query namespace, which is in the Microsoft.Office.Server.Search assembly.

The query is created and executed, and the results are displayed in the Render method. The first step is to construct the query to issue to the search engine. The query returns the title, path, and priority fields from all sites within the scope, for all task items where the assigned to field is equal to the current user’s name.

Create a FullTextSqlQuery object, setting the result types to relevant results, and setting the query text to the constructed query string. Execute the query by using the Execute method of the FullTextSqlQuery object. The method returns a result table collection, and the result table for the relevant results is then loaded into a data table.

To display the results, you use an SPGridView object. This is an Office SharePoint Server control that is based on the ASP.NET GridView control. The columns are not autogenerated, so you create a column in the SPGridView object from each column in the data table.

After creating the columns, you use the data table as the data source and bind it to the SPGridView object. Finally, you add the SPGridView object to the controls collection and call the RenderControl method to display the contents of the SPGridView object.

    protected override void Render(HtmlTextWriter writer)
    {
        string currentUser = SPContext.Current.Web.CurrentUser.Name;
        
        // Define the SQL statement.
        string searchQuery = "select title, path, priority" +
            " from scope()" +
            " where contentclass = 'STS_ListItem_Tasks'" +
            " and assignedto = '" + currentUser + "'";
        
        // Execute the query.
        FullTextSqlQuery query = new FullTextSqlQuery(SPContext.Current.Site);
        query.ResultTypes = ResultType.RelevantResults;
        query.QueryText = searchQuery;
        ResultTableCollection results = query.Execute();

        // Load the results into a datatable.
        ResultTable resultTable = results[ResultType.RelevantResults];
        DataTable table = new DataTable();
        table.Load(resultTable, LoadOption.OverwriteChanges);
        
        // Create the columns.
        SPGridView grid = new SPGridView();
        grid.AutoGenerateColumns = false;
        foreach (DataColumn column in table.Columns)
        {
            SPBoundField field = new SPBoundField();
            field.HeaderText = column.ColumnName;
                field.DataField = column.ColumnName;
            grid.Columns.Add(field);
        }

        // Bind the results to the grid.
        grid.DataSource = table;
        grid.DataBind();

        // Render the grid.
        Controls.Add(grid);
        grid.RenderControl(writer);
   }
Read It

Loading the search engine results into a DataTable object provides an easy way to bind and display the results in a grid. The query returns all task items that are assigned to the current user, regardless of the location of the task list. Using the search engine provides a very efficient method for retrieving this type of information if it resides across multiple site collections. In this example, you should implement pagination to ensure that the query and display of the search results perform well and efficiently.

You can extend the example in multiple ways to provide other useful functionality. For example, instead of using the pre-generated query, you can use input from the user to generate and execute the search query. Additionally, you can add code to enable the user to select the columns displayed in the grid results. You can also add column headers so that you can sort the results by a specific field.

See It

Displaying Search Results in a Grid View

Watch the Video

Video Length: 00:05:22

File Size: 3.61 MB WMV

Explore It

Community Additions

ADD
Show:
© 2014 Microsoft