|
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.
Get Updates By Email
Popular Post
- LINQ To SQL Tutorial
- LINQ To SQL Join On Multiple Conditions
- Code Sample: Programmatically Download File Using C#
- Free Icons And Images With Visual Studio 2008
- Windows 7 Control Panel In Classic Mode
- Dynamic Sort With LINQ
- Use SqlConnection With LINQ To SQL
- StyleCop Tutorial
- Write To Vista Event Log Using C#
- More Details Emerge On Microsoft Master Certification
Tag Cloud
Code Snippets
- Get Current Windows User In C#
- Get Width And Height Of Image In C#
- Get Windows Registry Size With WMI And C#
- Reverse Array Elements Using C#
- Convert Hexadecimal To Number In C#
- Get Free Disk Space Using T-SQL
- SQL Server 2008 – Get All Indexes In A Database
- Get Name Of Current Executing Assembly In C#
- Get CD Or DVD Drive Information Using WMI And C#
- Get Last Row From Table Using LINQ To SQL


February 28th, 2009 at 4:28 am
Thank you for your hard work! I will check back again shortly.
March 1st, 2009 at 10:39 am
Very good article, very informative. Keep up the great work.
March 14th, 2009 at 10:16 pm
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?
March 17th, 2009 at 9:56 am
Thank you for the information. You did help me!
March 17th, 2009 at 10:00 am
Tela,
Thanks for encouraging words.
March 19th, 2009 at 11:42 am
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?
March 19th, 2009 at 12:10 pm
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/
March 20th, 2009 at 3:48 pm
thanks very helpful post
March 20th, 2009 at 3:52 pm
June,
Thanks for the comment.
March 20th, 2009 at 3:58 pm
Thanks for keeping everyone abreast.