Click here to Skip to main content
13,410,869 members (30,801 online)
Click here to Skip to main content
Add your own
alternative version


5 bookmarked
Posted 1 Jun 2012

SharePoint 2010 – Search Documents Programmatically

, 1 Jun 2012
Rate this:
Please Sign up or sign in to vote.
How to use the Search Web Service to search documents from a Windows Forms application.


In this article I will demonstrate the usage of search from a Windows Forms context. Typically in a distributed scenario, there are multiple endpoints for retrieving data from SharePoint. Here internally I am using the SharePoint Search itself using the Search Web Service. So the same example can be extended to use with Library and List.

Step 1: Create a Document Library and upload files: You can create a Document Library inside the SharePoint site and name it Docs. Upload a sample document into it with some content.

Step 2: Create a Windows Forms application: Create a new Windows Forms application and add a web service reference to the following URL: http://YOURSERVER/_vti_bin/search.asmx. Please note to replace the server name with your server name. While adding the service reference, use the web service Add Reference dialog as shown below.

(Project > Add Reference > Advanced > Add Service Reference.)

Step 3: Add a search box and a button: Now you are ready to place a textbox, button, and grid on the form. The textbox allows the user to enter the search content and the button to do the search invocation.

Step 4: Execute the Search.

Now we are ready to add the search code on the button click event. Place the following code inside the button click event:

private void SearchButton_Click(object sender, EventArgs e)
    // setup service
    QueryService queryService = new QueryService();
    queryService.Credentials = System.Net.CredentialCache.DefaultCredentials;

    // set search string
    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(textBox1.Text); // user text

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

    // show result
    dataGridView1.DataSource = queryResults.Tables[0];

Here we are passing the search text as part of a Query Packet.  The result is received as a DataSet.

Step 5: Display the result.

Now try executing the application and enter a word to search for. The result will get displayed on the grid as shown below.

So this concludes our Search Service example with SharePoint. You can try uploading different documents, wait for the search service to crawl on it, and then search using the Windows application.


In this article, we have seen how to use the Search Web Service to search documents from a Windows Forms application. The attachment contains the example application which we have discussed. You need to modify the server name inside the application configuration file before executing the application.



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jean Paul V.A
United States United States
Jean Paul is a Microsoft MVP and Architect with 12+ years of experience. He is very much passionate in programming and his core skills are SharePoint, ASP.NET & C#.

In the academic side he do hold a BS in Computer Science & MBA. In the certification side he holds MCPD & MCTS spanning from .Net Fundamentals to SQL Server.

Most of the free time he will be doing technical activities like researching solutions, writing articles, resolving forum problems etc. He believes quality & satisfaction goes hand in hand.

You can find some of his work over here. He blogs at

You may also be interested in...


Comments and Discussions

QuestionGood article Pin
V.J. Kumar17-Jun-14 21:19
memberV.J. Kumar17-Jun-14 21:19 
GeneralMy vote of 3 Pin
venkat02498-Jan-14 21:51
membervenkat02498-Jan-14 21:51 
QuestionSoapException Pin
ashney14-Dec-12 2:37
memberashney14-Dec-12 2:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180225.1 | Last Updated 1 Jun 2012
Article Copyright 2012 by Jean Paul V.A
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid