In this article I will demonstrate the usage of search from a windows form 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 as Docs. Upload a sample document into it with some contents.
Step 2: Create a windows forms application
Create a new windows forms application and add web service reference to the following url.
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. Place the following code inside the button click.
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;
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.