Click here to Skip to main content
Click here to Skip to main content

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.

Introduction

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
    xmlString.Append("</QueryText></Context></Query></QueryPacket>");

    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.

Summary

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.

References

License

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

Share

About the Author

Jean Paul V.A
Architect
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 http://jeanpaulva.com
Follow on   Twitter

Comments and Discussions

 
QuestionGood article PinmemberV.J. Kumar17-Jun-14 20:19 
GeneralMy vote of 3 Pinmembervenkat02498-Jan-14 20:51 
QuestionSoapException Pinmemberashney14-Dec-12 1:37 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140826.1 | Last Updated 1 Jun 2012
Article Copyright 2012 by Jean Paul V.A
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid