How to: Submit a Keyword Query to Enterprise Search from a Client Application
Enterprise Search in Microsoft Office SharePoint Server 2007 includes the Query Web service, which exposes the search capabilities to client applications.
Note
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
In Visual Studio 2005, on the File menu, point to New, and then click Project.
In Project types, under C#, select Windows.
Under Templates, select Windows Application. In the Name field, type QueryExClientSample, and then click OK.
Create a proxy class for the Web service. You can do this in one of the following ways:
If you are working in Visual Studio 2005, follow the steps in How to: Create a Web Service Proxy Class for the Enterprise Search Query Web Service in Visual Studio 2005.
Use the Web Services Description Language Tool (Wsdl.exe), following the steps described in Creating an XML Web Service Proxy, then add a reference for the proxy class to the project for the client application.
To modify the default form for the client application
In Solution Explorer, double-click the form (Form1 if you are using the default form).
In the Toolbox, click Button, and then drag it to your form. In Properties, change (Name) to cmdQuery.
In the Toolbox, click TextBox, and then drag it to your form. In Properties, change (Name) to txtQuery, and set Multiline to True.
In the Toolbox, click DataGridView, and then drag it to your form. In Properties, change (Name) to grdResults.
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
Double-click cmdQuery to add an event handler for the Click event. The Code Editor opens with the cursor placed within the event handler.
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;
In the following code line, replace WebControl with WebPart.
public class clsSearchQuery : WebControl
Add the following line of code above the class declaration for clsSearchQuery.
[XmlRoot(Namespace = "CustomSearchWebPart")]
Example
//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'>"+
"urn:Microsoft.Search.Response.Document:Document</Format>"+
"</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
Tasks
Reference
Enterprise Search Keyword Syntax Reference
Concepts
Enterprise Search Query Web Service Overview