Home / Programming / Blog article: Writing Windows Forms Client for Live Search

| RSS

Writing Windows Forms Client for Live Search

February 4th, 2009 | 10 Comments | Posted in Programming

image 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:

  1. SOAP
  2. JSON
  3. 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.

image

On our form we will place three controls.

  1. Text Box to capture information we will search for.
  2. Button to initiate the search on Live Search
  3. Web Browser to display the search results.

 

The resulting form will look something like this:

 image

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:

image

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"

image

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.

image

 

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.

kick it on DotNetKicks.com

Leave a Reply 5566 views, 1 so far today |
Follow Discussion

10 Responses to “Writing Windows Forms Client for Live Search”

  1. C Jones Says:

    Thank you for your hard work! I will check back again shortly.

  2. nice! Says:

    Very good article, very informative. Keep up the great work.

  3. hydraulic jacks Says:

    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?

  4. Tela Z Says:

    Thank you for the information. You did help me!

  5. Deepak Says:

    Tela,

    Thanks for encouraging words.

  6. John Bailey Says:

    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?

  7. Deepak Says:

    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/

  8. June J Says:

    thanks very helpful post

  9. Deepak Says:

    June,

    Thanks for the comment.

  10. R Martinez Says:

    Thanks for keeping everyone abreast.

Leave a Reply





Switch to our mobile site