Displaying Search Results in a Grid View in SharePoint Server 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.
Applies to: 2007 Microsoft Office System, Microsoft Office SharePoint Server 2007
Microsoft Corporation
March 2008
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.
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 Overrides Sub Render(ByVal writer As HtmlTextWriter)
Dim currentUser As String = SPContext.Current.Web.CurrentUser.Name
' Define the SQL statement.
Dim searchQuery As String = "select title, path, priority" & _
" from scope()" & _
" where contentclass = 'STS_ListItem_Tasks'" & _
" and assignedto = '" & currentUser & "'"
' Execute the query.
Dim query As FullTextSqlQuery & _
= New FullTextSqlQuery(SPContext.Current.Site)
query.ResultTypes = ResultType.RelevantResults
query.QueryText = searchQuery
Dim results As ResultTableCollection = query.Execute()
' Load the results into a datatable.
Dim resultTable As ResultTable & _
= results(ResultType.RelevantResults)
Dim table As DataTable = New DataTable()
table.Load(resultTable, LoadOption.OverwriteChanges)
' Create the columns.
Dim grid As SPGridView = New SPGridView()
grid.AutoGenerateColumns = False
Dim column As DataColumn
For Each column In table.Columns
Dim field As SPBoundField = New SPBoundField()
field.HeaderText = column.ColumnName
field.DataField = column.ColumnName
grid.Columns.Add(field)
Next
' Bind the results to the grid.
grid.DataSource = table
grid.DataBind()
' Render the grid.
Controls.Add(grid)
grid.RenderControl(writer)
End Sub
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. |
Video Length: 00:05:22 File Size: 3.61 MB WMV
|
