How to: Submit a Keyword Query to Windows SharePoint Services Search from a Client Application
This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
Search in Windows SharePoint Services 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 ASP.NET Web applications, Windows Form applications, etc.
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.
Procedures
To submit a keyword query to Windows SharePoint Services Search from a client application
Create a proxy class for the Web service.
If you are working in Visual Studio 2005, you can follow the steps outlined in How to: Create a Web Service Proxy Class for the Windows SharePoint Services Search Web Service in Visual Studio.
Alternatively you can use the Web Services Description Language Tool (Wsdl.exe), using 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
Create a string variable and set it to the XML query string containing the keyword search query you want to pass to the search service.
Create an instance of the Web service proxy class.
If needed, specify the credentials to pass to the Web service.
Call the QueryEx Web method of the Query Web service, storing the returned results in a System.Data.DataSet.
Code
//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];