Click here to Skip to main content
15,885,985 members
Articles / Programming Languages / C#
Tip/Trick

SharePoint 2010 – Search Documents Programmatically

Rate me:
Please Sign up or sign in to vote.
3.00/5 (1 vote)
1 Jun 2012CPOL2 min read 36.6K   398   5   3
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.

Image 1

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.)

Image 2

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.

Image 3

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:

C#
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.

Image 4

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)


Written By
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

Comments and Discussions

 
QuestionGood article Pin
Vijaykumar Vadnal17-Jun-14 20:19
Vijaykumar Vadnal17-Jun-14 20:19 
GeneralMy vote of 3 Pin
venkat02498-Jan-14 20:51
professionalvenkat02498-Jan-14 20:51 
QuestionSoapException Pin
ashney14-Dec-12 1:37
ashney14-Dec-12 1: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.