Friday, August 31, 2012

Working with SharePoint 2010 search web service

In this article we will discuss how to user search web service in SharePoint 2010. We will search by using SharePoint search web service. Also you can check about FAST search hereSharePoint 2010 search is very useful and advanced in SharePoint 2010.

There are different search web services available for SharePoint search and SharePoint Enterprise Search. The service url for sharepoint search is:


The web service url for sharepoint enterprise search is:

For this demo we will use client object model and we will bind the search result to a datagridview in a windows application.

You can put a Textbox where user can put the search text, a button and a datagridview where user can see the search result as output. see the figure for reference.
In the next step we need to add reference to the web service.

For this Right click on the project and click Add service reference. This will open the Add Service Reference dialog box. On that box click on Advanced... as shown in the figure below:

This will open the Service Reference Settings dialog box, from there click on Add Web Reference... as shown in the figure below.

The above step will open the Add Web Reference dialog box.
In that window enter the URL of the web service and click on the green arrow. This will search and found the web service if present. Then click on Add Reference as shown in the figure below:

This finishes our reference to the web service.

Now open the Button click code.

First give reference to the web service like:

using SearchPOC.QueryWebServiceProxy;

Here we will use the QueryService class and pass the DefaultCredentials as the parameter.

Then it will take an XML as an search input parameter which is shown in the full code below.

Then here in this example we are calling the QueryEx method which returns the results as a DataSet. We also can use Query method which returns results as an XML document.

Full code:

private void btnSearch_Click(object sender, EventArgs e)
            QueryService queryService = new QueryService();

            queryService.Credentials = System.Net.CredentialCache.DefaultCredentials;

            StringBuilder queryXml = new StringBuilder();

            queryXml.Append("<QueryPacket xmlns="urn:Microsoft.Search.Query" Revision="1000">");

            queryXml.Append("<Query domain="QDomain">");










            queryXml.Append("<QueryText language="en-US" type="STRING">");






            System.Data.DataSet queryResults = queryService.QueryEx(queryXml.ToString());

            dataGridView1.DataSource = queryResults.Tables[0];
            catch (Exception ex)

                string s = ex.InnerException.ToString();