Writing Windows Forms Client for Live Search
In this article we will look at how to write a Windows Forms Client to access Microsoft Live Search API. Via this API you can use the power of a giant search engine within your application. While developing our application we will see that the overall design of the API is simple and easy to work with. We will create a Windows Forms Application which will access Live Search.
Basics
To access Live Search through our application we will need an Application ID which can be obtained from Live Search Developer Center. The process requires us to sign in with Live ID and then fill out a form telling Microsoft about our application. Once we have submitted the form an Application ID is issued immediately. This ID is required to query WSDL and access the web services from our application.
Live Search API 2.0 can be accessed via three protocols:
- SOAP
- JSON
- XML
For this article we will work with SOAP protocol.
Creating our application
We will follow the standard process of creating a Windows Forms application by selecting Windows Forms Application as a template.
On our form we will place three controls.
- Text Box to capture information we will search for.
- Button to initiate the search on Live Search
- Web Browser to display the search results.
The resulting form will look something like this:
To access the Live Search API which is exposed as a web service we will add a Service Reference in our project. Service References can be added by right clicking on the project in solution explorer and then by clicking Add Service Reference. This brings the following dialog:
In the address box we will enter the location of WSDL for Live Search Web Service. The address is
http://api.search.live.net/search.wsdl?AppID=
Before entering the address we need to append our application ID to the URL.
http://api.search.live.net/search.wsdl?AppID=<substitute this with your application ID>
After entering the address and hitting Go, Visual Studio comes back with a list of services available at the address we entered. In our case it finds one service called "LiveSearchService"
Things look good so we will change the default namespace to LiveSerchClient and click OK. Visual Studio at this stage generates a proxy and creates a App.config file which contains address, bindings and contract information required to access the service.
Most of the work our application does happens in the click event of the button. Within the event handler we will create objects required to conduct the search and receive a response.
private void btnSearch_Click(object sender, EventArgs e)
{
// Create an instance for client which is used
// to make web service calls
LiveSearchPortTypeClient client = new LiveSearchPortTypeClient();
// SearchRequest is used to define search parameters
SearchRequest request = new SearchRequest();
// assign the required Application Id
request.AppId = appId;
// we are conducting a web search
request.Sources = new SourceType[] { SourceType.Web };
// thing we are searching for
request.Query = txtSearchFor.Text;
// response we recieve from Live Search is stored here
SearchResponse response = client.Search(request);
// Display the results
DisplayResults(response.Web.Results);
}
Once we have received our results we will use two helper methods to display the results in Web Browser control.
private void DisplayResults(WebResult[] results)
{
StringBuilder sb = new StringBuilder();
results.ToList().ForEach(x => sb.Append(GetFormattedResult(x)));
webResults.DocumentText = sb.ToString();
}
private string GetFormattedResult(WebResult result)
{
string formattedResult = "<a href=" + result.Url + ">"
+ result.Title + "</a><br/>"
+ result.Description + "<br/><br/>";
return formattedResult;
}
Running the application and doing some ego-surfing we see our search results displayed in our application.
Is this all Live Search Offers?
The answer is no. Live Search API exposes much more functionality that what we saw here. Other than web searches we can also search for images, news etc. individually or by combining them in one search request. If you application needs to search the web then Live Search can be used very easily.
Conclusion
In this article we saw how easy it is to programmatically access Microsoft Live Search. You are not limited to just Windows Forms applications. Live Search can be used in any type of application such as Silverlight, ASP.NET, WPF or Class Library. This article gave you a brief introduction to Live Search API using Windows Forms Client as an example. I hope you enjoyed reading it.
10 Responses to Writing Windows Forms Client for Live Search
Leave a Reply Cancel reply
Top Posts
- LINQ To SQL Tutorial
- LINQ To SQL Join On Multiple Conditions
- Code Sample: Programmatically Download File Using C#
- Windows 7 Control Panel In Classic Mode
- More Details Emerge On Microsoft Master Certification
- Use SqlConnection With LINQ To SQL
- Free Icons And Images With Visual Studio 2008
- Capture XML In WCF Service
- Dynamic Sort With LINQ
- StyleCop Tutorial
Tags
.Net 2010 ADO.NET ASP.NET Azure Blogging Books Browsers C# Certification Cloud Computing Code Snippets Community Data Services Eclipse Entity Framework Google IDE Java LINQ Mac Microsoft Museum NetBeans Office Oracle REST SharePoint Silverlight SQL Server T-SQL Tips Tools Training Visual Studio Visual Studio 2010 WCF Web Windows Windows 7 Windows Forms Windows Live WMI WPF XAML


Thank you for your hard work! I will check back again shortly.
Very good article, very informative. Keep up the great work.
This is the first time I comment here and I should say that you share genuine, and quality information for other bloggers! Good job.
p.s. You have a very good template . Where did you find it?
Thank you for the information. You did help me!
Tela,
Thanks for encouraging words.
I had developed a MS Word addin that used Live Search some time ago, but I had difficulty getting it licensed. The licensing model for Live Search was designed around web sites and how many searches they did and there was no way for me to know this for a Windows client. Has this changed? If not, how do you license desktop clients to use live search?
Hi John,
Because I only recently started looking at Live Search so I am not aware of earlier licensing model. At present all you need is an App ID for your application to work. This can easily be obtained from this link. http://search.live.com/developers/
thanks very helpful post
June,
Thanks for the comment.
Thanks for keeping everyone abreast.