Friday, August 31, 2012


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:

http://[URL]/_vti_bin/spsearch.asmx

The web service url for sharepoint enterprise search is:
http://[URL]/_vti_bin/search.asmx

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)
        {
            try
            {                       
            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("<SupportedFormats>");

            queryXml.Append("<Format>");

            queryXml.Append("urn:Microsoft.Search.Response.Document.Document");

            queryXml.Append("</Format>");

            queryXml.Append("</SupportedFormats>");

            queryXml.Append("<Range>");

            queryXml.Append("<Count>5000</Count>");

            queryXml.Append("</Range>");

            queryXml.Append("<Context>");

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

            queryXml.Append(txtSiteURL.Text);

            queryXml.Append("</QueryText>");

            queryXml.Append("</Context>");

            queryXml.Append("</Query>");

            queryXml.Append("</QueryPacket>");

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

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

                string s = ex.InnerException.ToString();
            }

        }

1 Comments:

  1. Thanks a lot for sharing this useful piece of information. This is really informative and quite interesting. Great posting!
    AccurateAppend.com

    ReplyDelete