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
private void SearchButton_Click(object sender, EventArgs e)
QueryService queryService = new QueryService();
queryService.Credentials = System.Net.CredentialCache.DefaultCredentials;
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'>");
System.Data.DataSet queryResults = queryService.QueryEx(xmlString.ToString());
dataGridView1.DataSource = queryResults.Tables;
Here we are passing the search text as part of a Query Packet. The result is received as a
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.
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