Walkthrough: Creating a Basic Search Web Part Using the Query Object Model
Applies to: SharePoint Server 2010
In this article
Setting Up the Web Part Project
Implementing the Web Part
Deploying and Testing the Web Part
In Microsoft SharePoint Server 2010, you can customize the appearance and functionality of the Search Center pages and Web Parts from the browser. However, if you want to customize these pages or Web Parts in ways that are not possible through the browser, you can create custom Web Parts that use the Query object model or Federation object model to execute search queries.
In this walkthrough, you will create a custom Web Part that executes a keyword query and displays the search results returned using the Query object model. For an example that shows how to create a custom search Web Part by using the Federation object model, see Walkthrough: Creating a Basic Search Web Part Using the Federation Object Model.
You can find the complete code for the CustomKeywordSearch Web Part sample in Code Sample: Custom Keyword Search Web Part Code.
Note
The Web Part described in this walkthrough provides very basic search functionality.
This walkthrough addresses the following tasks:
Setting up the Web Part project
Implementing the Web Part
Deploying and testing the Web Part
Prerequisites
To perform this walkthrough, you must have the following installed on your development computer:
Microsoft SharePoint Server 2010
Microsoft Visual Studio 2010 or a similar Microsoft .NET Framework-compatible development tool
Setting Up the Web Part Project
To create the project for the Web Part
In Visual Studio 2010, on the File menu, point to New, and then click Project.
In Project types, under C#, select SharePoint.
Under Templates, select Empty SharePoint Project. In the Name field, type CustomKeywordSearch, and then click OK.
In the SharePoint Customization Wizard, click Deploy as a farm solution, and then click Finish.
Next, you must add the required references to your Web Part project.
To add references to the Web Part project
On the Project menu, click Add Reference.
On the .NET tab, select each of the following references, and then click OK after each selection:
Microsoft.Office.Server
Microsoft.Office.Server.Search
Before you add code for the Web Part, you must add the Web Part class file to the project, and then add the using statements to the class.
To add the Web Part class to the project
On the Project menu, click Add New Item.
In the Add New Item dialog box, click Web Part, type Custom Keyword Search, and then click Add.
Add the following namespace directives at the beginning of the class.
using System.Data; using Microsoft.Office.Server.Search.Query; using Microsoft.Office.Server.Search.Administration;
Implementing the Web Part
Now you can write the code to execute the query, and then render the search results that are returned.
To add the Web Part's child controls and render them
Add the following code below the Custom_Keyword_Search class declaration.
Button queryButton; TextBox queryTextBox; Label resultsLabel; DataGrid resultsGrid;
In the CreateChildControls method, add the code to initialize the controls, as follows.
protected override void CreateChildControls() { Controls.Clear(); queryTextBox = new TextBox(); this.Controls.Add(queryTextBox); queryButton = new Button(); queryButton.Text = "Start Search"; queryButton.Click += new EventHandler(queryButton_Click); this.Controls.Add(queryButton); resultsLabel = new Label(); this.Controls.Add(resultsLabel); }
Using the following code, add a click event for queryButton.
void queryButton_Click(object sender, EventArgs e) { if (queryTextBox.Text != string.Empty) { ExecuteKeywordQuery(queryTextBox.Text); } else { resultsLabel.Text = "You must enter a search word."; } }
Add the following code for the ExecuteKeywordQuery method.
void ExecuteKeywordQuery(string queryText) { SearchServiceApplicationProxy proxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy (SPServiceContext.GetContext(SPContext.Current.Site)); KeywordQuery query = new KeywordQuery(proxy); query.ResultsProvider = Microsoft.Office.Server.Search.Query.SearchProvider.Default; query.QueryText = queryText; query.ResultTypes|=ResultType.RelevantResults; ResultTableCollection searchResults = query.Execute(); if(searchResults.Exists(ResultType.RelevantResults)) { ResultTable searchResult = searchResults[ResultType.RelevantResults]; DataTable result = new DataTable(); result.TableName = "Result"; result.Load(searchResult,LoadOption.OverwriteChanges); FillResultsGrid(result); } }
Add the following code for the FillResultsGrid method.
private void FillResultsGrid(DataTable resultTable) { //Instantiate the DataGrid resultsGrid = new DataGrid(); //Set the DataSource resultsGrid.DataSource = resultTable; //Bind the data to the DataGrid resultsGrid.DataBind(); //Add the DataGrid to the controls Controls.Add(resultsGrid); }
Now you can deploy the Web Part.
Deploying and Testing the Web Part
To deploy the Web Part
On the Build menu, click Deploy Solution.
The deploy process does the following:
Builds the Web Part DLL and deploys it to the global assembly cache.
Creates the Web Part and deploys it to the Web site.
Recycles Internet Information Services (IIS).
Reselecting Deploy Solution after you make changes to the project automatically retracts the previous version of the solution and replaces it with the newest version.
From the browser, navigate to the Web Parts page you want to add the Web Part to.
Click the Site Actions tab, and then click Edit Page.
Click Add a Web Part in any Web Part zone.
In Categories, click Custom.
In Web Parts, click Custom Keyword Search, and then click Add.
Click Save & Close. Verify that the Custom Keyword Search Web Part appears on the page.
To test the Web Part
Type a search term in the text box, and then click Start Search.
In Visual Studio 2010, on the Project menu, click CustomKeywordSearch Properties.
On the Debug tab, in the Start Action section, click Start browser with URL, and then type the URL to the Web Parts page that you added the Web Part to. Close the Properties page.
After you complete this step, you can set a breakpoint in the Web Part code and start debugging by clicking F5 on the Debug menu.