Walkthrough: Querying SharePoint Search From a Client Application
Applies to: SharePoint Server 2010
Note
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 SharePoint Server search results to a Windows-based client application by using the QueryEx Web method, and includes the following tasks:
Setting up the client application
Referencing the Query web service from the client application
Modifying the form for the client application
Coding the client application
Testing the client application
To perform this walkthrough, ensure the following:
Microsoft Visual Studio 2010 is installed on your development computer.
You have permissions to access a SharePoint site configured to use SharePoint Server search.
To set up the client application
In Visual Studio 2010, on the File menu, point to New, and then click Project.
Under Installed Templates, expand Visual C#, and then click Windows.
Select Windows Forms Application. In the Name field, type QueryExClientSample, and then click OK.
Add a Web reference to Query Web service
In Solution Explorer, right-click the name of your project, and then click Add Service Reference.
In the Add Service Reference dialog box, click Advanced.
In the Service Reference Settings dialog box, click Add Web Reference.
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.
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
In Solution Explorer, double-click the form (Form1 if you are using the default form).
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.
In the Toolbox, click TextBox, and then drag the control to your form. In Properties, change (Name) to queryTextBox, and set Multiline to True.
In the Toolbox, expand Data, click DataGridView, and then drag the control to your form. In Properties, change (Name) to resultsGrid.
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.
To write the code for the client application
Double-click queryButton to add an event handler for the Click event. The Code Editor opens with the cursor placed within the queryButton_Click event handler.
Add the following code to the queryButton_Click event
try { // Instantiate the Web service. QueryWebServiceProxy.QueryService queryService = new QueryWebServiceProxy.QueryService(); // Use the credentials of the user running the client application. queryService.Credentials = System.Net.CredentialCache.DefaultCredentials; //Execute the QueryEx method, returning the results to a DataSet System.Data.DataSet queryResults = queryService.QueryEx(GetXMLString()); // Set the DataGridView data source to the first table in the DataSet oject, which contains the relevant results. resultsGrid.DataSource = queryResults.Tables[0]; } catch (Exception ex) { resultsLabel.Text = ex.ToString(); }
The string passed to the QueryEx method is constructed in the GetXMLString function. Add the following code to the Form1 class to create the GetXMLString function.
private string GetXMLString() { /* The proceeding six lines of code is actually one line of code. It is separated into four lines here for readability. You will need to remove the line breaks when you copy the code to your project. */ StringBuilder xmlString = new StringBuilder("<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'>"); xmlString.Append(queryTextBox.Text); xmlString.Append("</QueryText></Context></Query></QueryPacket>"); return xmlString.ToString(); }
To test the client application
Press F5 to build and run the client application.
Type a keyword query into the textbox. See the Keyword Query Syntax Reference for information on how to construct keyword queries.
Click Query to submit the keyword query to the Query web service. If results are returned, they will be displayed in the DataGridView control.
See Also
Reference
QueryService