How to: Submit a Keyword Query to Enterprise Search from a Client Application

SharePoint 2007

Enterprise Search in Microsoft Office SharePoint Server 2007 includes the Query Web service, which exposes the search capabilities to client applications.


In this context, client application refers to applications calling the Query Web service. This could include Microsoft ASP.NET Web applications, Windows Forms applications, and so on.

The QueryEx Web method of the Query Web service sends a query to the search service, and returns the results in a System.Data.DataSet object. The following procedure shows how to use the Enterprise Search Query Web service to return search results to a Windows–based client application using the QueryEx Web method, and includes the following tasks:

  • Setting up the client application

  • Modifying the form for the client application

  • Writing the code

To perform this procedure, ensure the following:

  • Microsoft Visual Studio 2005 is installed on your development computer.

  • You have permissions to access a SharePoint site configured to use Enterprise Search.

To set up the Windows–based client application

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

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

  3. Under Templates, select Windows Application. In the Name field, type QueryExClientSample, and then click OK.

  4. Create a proxy class for the Web service. You can do this in one of the following ways:

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. In the Toolbox, click Button, and then drag it to your form. In Properties, change (Name) to cmdQuery.

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

  4. In the Toolbox, click DataGridView, and then drag it to your form. In Properties, change (Name) to grdResults.

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

To write the code for the client application

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

  2. Add the following namespace directives near the top of the code in clsSearchQuery.cs.

    using System.Drawing;
    using System.Data;
    using System.Xml;
    using System.Xml.Serialization;
    using Microsoft.SharePoint.WebPartPages;
    using Microsoft.Office.Server;
    using Microsoft.Office.Server.Search.Query;
  3. In the following code line, replace WebControl with WebPart.

    public class clsSearchQuery : WebControl
  4. Add the following line of code above the class declaration for clsSearchQuery.

    [XmlRoot(Namespace = "CustomSearchWebPart")]


//The string containing the keyword to use in the search
string keywordString = "Microsoft";

//The XML string containing the query request information 
//for the Web service
string qXMLString = "<QueryPacket xmlns='urn:Microsoft.Search.Query'>"+
"<Query><SupportedFormats><Format revision='1'>"+
"</SupportedFormats><Context><QueryText language='en-US' type='STRING'>"+
keywordString + "</QueryText></Context></Query></QueryPacket>";

QueryWebServiceProxy.QueryService queryService = new QueryWebServiceProxy.QueryService();
queryService.Credentials = System.Net.CredentialCache.DefaultCredentials;
System.Data.DataSet queryResults = queryService.QueryEx(qXMLString);
resultsGridView.DataSource = queryResults .Tables[0];

See Also

Community Additions