Walkthrough: Querying FAST Search Server From a Client Application

The Query Web service exposes the SharePoint Enterprise Search capabilities in Microsoft SharePoint Server 2010 to client applications.

Note

In this article
Prerequisites
Step 1: Set up and configure the forms-based client application
Step 2: Write the code for the client application
Step 3: Test the client application
In this context, client application refers to applications that call the Query Web service. This can include applications such as Microsoft ASP.NET Web applications or Windows Forms applications.

The QueryEx Web method of the Query Web service sends a query to the search service, and returns the results in a DataSet object. The following walkthrough describes how to use the Query Web service to return FAST Search Server 2010 for SharePoint results to a Windows-based client application by using the QueryEx Web method, and includes the following tasks:

This walkthrough is an extension of Walkthrough: Querying SharePoint Search From a Client Application. The Visual Studio 2010 project adds a second DataGridView control, and the code example uses the FAST Query Language (FQL) query syntax.

Applies to: SharePoint Server 2010

Prerequisites

To complete this walkthrough, ensure the following:

  • Microsoft Visual Studio 2010 is installed on your development computer

  • You have permissions to access a SharePoint Server 2010 site configured to use FAST Search Server 2010 for SharePoint

Step 1: Set up and configure the forms-based client application

You set up a forms-based client application in Visual Studio 2010, which includes the following elements:

  • A text box for query input.

  • A query button.

  • A DataGridView control for the query result table.

  • A DataGridView control for additional query result tables. The control is not used in the code example of this article, but is used in other articles such as Query Refinement (FAST Search Server 2010 for SharePoint).

To set up the Visual Studio project

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

  2. Under Installed Templates, expand Visual C#, and then click Windows.

  3. Select Windows Forms Application. In the Name field, type QueryExClientSample, and then click OK.

To add a Web reference to the Query Web service

  1. In Solution Explorer, right-click the name of your project, and then click Add Service Reference.

  2. In the Add Service Reference dialog box, click Advanced.

  3. In the Service Reference Settings dialog box, click Add Web Reference.

  4. In the Add Web Reference dialog box, in the URL text field, type the following address: http://SERVER/_vti_bin/search.asmx. Replace SERVER with the URL to the SharePoint site, and then click Go.

  5. When the Web service is located, the page for QueryService Web service is displayed in the main window of the Add Web Reference dialog box. Type QueryWebServiceProxy in the Web reference name field, and then click Add Reference.

To modify the default form for the client application

  1. In Solution Explorer, double-click the form (Form1 if you are using the default form).

  2. Add a query button:

    In the Toolbox, expand Common Controls, click Button, and then drag the control to your form. In Properties, change (Name) to queryButton, and type Query in the Text property.

  3. Add a query input box:

    In the Toolbox, click TextBox, and then drag the control to your form. In Properties, change (Name) to queryTextBox, and set Multiline to True.

  4. Add a DataGridView control for the query results table:

    In the Toolbox, expand Data, click DataGridView, and then drag the control to your form. In Properties, change (Name) to resultsGrid.

  5. Add a second DataGridView control for additional query result tables:

    In the Toolbox, expand Data, click DataGridView, and then drag the control to your form. In Properties, change (Name) to secondGrid.

  6. Add a Label control for error messages:

    In the Toolbox, click Label, and then drag the control to your form. In Properties, change (Name) to resultsLabel, and then delete the contents of the Text property.

Step 2: Write the code for the client application

The code implements a query handler associated with the queryButton you have configured. The code performs the following tasks:

  • Set up the Query Web service connection

  • Create the Query XML

  • Pack the user-entered query in an FQL STRING operator

  • Run the query using the QueryEx Query Web service method

  • Display the query results in the first DataGridView control

  • Print the extended properties for the query result in the MessageBox

To write the code for the client application

  1. Double-click the query button to add an event handler for the Click event. The Code Editor opens with the cursor placed within the queryButton_Click event handler.

  2. Add the following namespace reference.

    using System.Collections;
    
  3. Add the following code to the queryButton_Click event.

    try
    {
       // Instantiate the Query Web service.
       QueryWebServiceProxy.QueryService queryService = new QueryWebServiceProxy.QueryService();
       // Use the credentials of the user running the client application: 
       queryService.Credentials = System.Net.CredentialCache.DefaultCredentials;
       // Run the QueryEx method, returning the results to a DataSet:
       System.Data.DataSet queryResults = queryService.QueryEx(GetXMLString());
       // Set the first DataGridView data source to the RelevantResults table in the DataSet object:
       resultsGrid.DataSource = queryResults.Tables["RelevantResults"];
    
       // Print extended properties for the result set in a message box.
       StringBuilder props = new StringBuilder("Extended properties on result object:\n");
       foreach (DictionaryEntry de in queryResults.ExtendedProperties)
       {
          props.AppendFormat("{0}: {1}\n", de.Key, de.Value);
       }
       props.Append("\nExtended properties on RelevantResults:\n"); 
       foreach (DictionaryEntry de in queryResults.Tables["relevantresults"].ExtendedProperties)
       {
          props.AppendFormat("{0}: {1}\n", de.Key, de.Value);
       }
       MessageBox.Show(props.ToString());
     }
    catch (Exception ex)
    {
       resultsLabel.Text = ex.ToString();
    }
    
  4. You construct the query XML string passed to the QueryEx method in the GetXMLString function.

    Add the following code to the Form1 class to create the GetXMLString function.

    // Build the query XML string.
    private string GetXMLString()
    {
       // queryXML1 is the part of the XML before the query string.
       string queryXML1 = @"<QueryPacket xmlns='urn:Microsoft.Search.Query'>
       <Query>
          <SupportedFormats>
             <Format revision='1'> urn:Microsoft.Search.Response.Document:Document</Format>
          </SupportedFormats>
          <Context>
             <QueryText language='en' type='FQL'>";
    
       // queryXML2 is the part of the XML after the query string.
       string queryXML2 = @"
             </QueryText>
          </Context>
          <ResultProvider>FASTSearch</ResultProvider>
          <Range>
             <Count>10</Count>
          </Range>
       </Query>
    </QueryPacket>";
    
    // Build the Query XML string.
       StringBuilder xmlString = new StringBuilder(queryXML1); 
       xmlString.Append(GetFQLString());
       xmlString.Append(queryXML2);
       return xmlString.ToString();
    }
    
    // Build the FQL query string.
    // string("<user-typed query>", mode="and")
    private string GetFQLString()
    {
       StringBuilder fqlString = new StringBuilder("string(\"");
       fqlString.Append(queryTextBox.Text);
       fqlString.Append("\", mode=\"and\")");
       return fqlString.ToString();
    }
    

Step 3: Test the client application

You test the client application by typing one or more query terms in the query box. The query represents an AND of the terms you have typed.

To test the client application

  1. Press F5 to build and run the client application.

  2. Type one or more words into the text box.

  3. Click Query to submit the query to the Query Web service. If results are returned, they will be displayed in the first DataGridView control. The extended properties will be displayed in the MessageBox pop-up window.

See Also

Reference

QueryService

Concepts

Using the Query Web Service

Walkthrough: Querying SharePoint Search From a Client Application

FAST Query Language (FQL) Syntax Reference

Building Search Queries

Other Resources

Microsoft.Search Schema Reference