Export (0) Print
Expand All

Walkthrough: Creating a Basic Search Web Part Using the Query Object Model

Last modified: April 30, 2010

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 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

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

To create the project for the Web Part

  1. In Visual Studio 2010, on the File menu, point to New, and then click Project.

  2. In Project types, under C#, select SharePoint.

  3. Under Templates, select Empty SharePoint Project. In the Name field, type CustomKeywordSearch, and then click OK.

  4. 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

  1. On the Project menu, click Add Reference.

  2. 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

  1. On the Project menu, click Add New Item.

  2. In the Add New Item dialog box, click Web Part, type Custom Keyword Search, and then click Add.

  3. 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;
    

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

  1. Add the following code below the Custom_Keyword_Search class declaration.

    Button queryButton;
    TextBox queryTextBox;
    Label resultsLabel;
    DataGrid resultsGrid;
    
  2. 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);
    }
    
  3. 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.";
         }
    }
    
  4. 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);
         }
    }
    
  5. 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.

To deploy the Web Part

  1. 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.

  2. From the browser, navigate to the Web Parts page you want to add the Web Part to.

  3. Click the Site Actions tab, and then click Edit Page.

  4. Click Add a Web Part in any Web Part zone.

  5. In Categories, click Custom.

  6. In Web Parts, click Custom Keyword Search, and then click Add.

  7. Click Save & Close. Verify that the Custom Keyword Search Web Part appears on the page.

To test the Web Part

  1. Type a search term in the text box, and then click Start Search.

  2. In Visual Studio 2010, on the Project menu, click CustomKeywordSearch Properties.

  3. 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.

Community Additions

ADD
Show:
© 2014 Microsoft