Windows Live Search API: a search engine in your website

Ronald Vasseur

Windows Live Search is the Microsoft search engine. This tool is one of the Microsoft's answers to Google in the search's engines war. This search engine offers equals performances that of Google with a very good quality results level. Despite the power of Microsoft, this search engine is virtually non-existent address the monopoly of Google, in fact, the latter represents more than 90% of the market.

- Titre : Windows Live Search API: a search engine in your website

- Auteur : Ronald Vasseur

- Parution : 4 septembre 2008

- Licence : Les sources présentées sur cette page sont libres de droits et vous pouvez les utiliser à votre convenance. Par contre, la page de présentation constitue une œuvre intellectuelle protégée par les droits d'auteur. Copyright ® 2008 Ronald Vasseur. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents, images, etc. sans l'autorisation expresse de l'auteur. Sinon vous encourez selon la loi jusqu'à trois ans de prison et jusqu'à 300 000 € de dommages et intérêts.

Sommaire

- 1. Windows Live Search

- 2. Windows Live Search API, the Web Service

- 2-1. How to get an application ID ?

- 3. Live Search API

- 3-1. Main classes and properties

- 4. Use of Windows Live Search in your website

- 5. Basic search

- 6. Advanced search

- Conclusion

- Ressources

- Liens vers le MSDN

- Liens vers Developpez.com

1 - Windows Live Search

Windows Live Search is the Microsoft search engine. This tool is one of the Microsoft's answers to Google in the search's engines war. This search engine offers equals performances that of Google with a very good quality results level. Despite the power of Microsoft, this search engine is virtually non-existent address the monopoly of Google, in fact, the latter represents more than 90% of the market.

The Microsoft search engine is available here: http://www.live.com

Live is now the Microsoft's brand for denote all online services: search engine, instant messaging, tools...

2 - Windows Live Search API, the Web Service

Windows Live Search exposes a SOAP Web Service that everybody can access for request the search engine. This Web Service is free for both personal and commercial use up to 25K requests per day (above 25K requests per day only on the search function is already a quite big website!). This Web Service will allow you to make research for pages but not only, also for images, news, phonebook, location...

This service is SOAP based which means that almost every platform can reach it: PHP, Java, .Net, Python... The SOAP protocols use HTTP on port 80 to communicate; it means the connection won't be avoided by any firewall.

The Live Search Web Service WSDL contract is available at this URL: http://soap. search.live.com/webservices.asmx?wsdl

Prerequisites to access to the Windows Live Search Web Service:

- Own a Windows Live ID

- Own an application ID

2-1 - How to get an application ID ?

It's pretty easy; you just need to register online at this URL: http://search.live.com/developer. After you will be authenticated on this website with a Windows Live account you will be invited to fill a field, accept the "terms of use" and that's all. You will get your Application ID immediately, be careful this ID can needs up to 15 minutes after creation to be active. This ID will be included in your application to indentify which website is requesting the Live Search Web Service.

3 - Live Search API

We are going to see in how is structured the Live Search API. We are going to pay attention to the most important classes and methods.

3-1 - Main classes and properties

Let's start with the SearchRequest class. The SearchRequest class offers useful properties that we can use to parameterize our search as we want. Four of these properties are required; others are optional, let's see in details what these properties are related with.

Required fields

- AppID : ID generated by Microsoft after you registered.

- CultureInfo : the culture of your search, it allow you to make really accurate search.

- Query: this is the main property for a search, this string field will contains the content of our search, and concretely it will be the text searched by users. This property is typed as a System.String.

Optional fields

- Flags: define different option to parameter the request.

- Location: use it to define a location (longitude, latitude, radius…).

- SafeSearch : filter to avoid explicit content, could be disabled, moderate or strict.

Now let's see the SourceRequest class. The source property of this class permits to specify which sources we are going to request. Different sources (SourceType) are available:

- Ads

- Image

- InlineAnswers

- News

- PhoneBook

- QueryLocation

- Spelling

- Video

- Web

- WordBreaker (reserved for future use)

The SourceRequest allow making request to different sources in same times, it means it's possible for the same request to get a response containing results from web, images, news... It's really possible to create a powerful search function.

If wanted it's also possible to limit the research at only one domain, for example if we want to search pages, images... on the www.cnn.com we simply add "site:www.cnn.com" after the query itself. The complete query will be "Beijing 2008 site:www.cnn.com".

4 - Use of Windows Live Search in your website

In this part we will see how to include Windows Live Search Web Service directly into an ASP.Net website. Let's proceed right now:

- Launch Visual Studio 2008

- Add an empty solution

- Add an ASP.Net website to the current solution

- Add a new page to the current solution

- Add a Web Reference to the ASP.Net Website

- The Web Service exposes a WSDL contract at this address : http://soap.search.live.com/webservices.asmx?wsdl

- Rename this reference like "LiveSearchWS"

As you can see this Web Service expose only one method, Search(). This method takes a SearchRequest object as parameter and return a SearchResponse. Now, you can use Live Search API directly into your application. Let's see how to proceed!

5 - Basic search

At this point you don't need to do anything else for use the Windows Live Search SOAP Web Service, it's referenced like any other Web Services. Now we are going to send a request to the Web Service and get the returned result. Here is the C# code used to send a basic search request:

Basic search

public void BasicRequest()
{
// Live Search Service
MSNSearchService liveSearchSvc = new MSNSearchService();

// Request object
SearchRequest request = new SearchRequest();

// 'sr' contains types of sources we are going to request (pages, videos, images...)
SourceRequest[] sr = new SourceRequest[1];

// Request the web pages, with all fields in response
sr[0] = new LiveSearchWS.SourceRequest();
sr[0].Source = LiveSearchWS.SourceType.Web;
sr[0].ResultFields = ResultFieldMask.All;

// Parameters of the request
request.Query = "Beijing 2008";
request.Flags = SearchFlags.None;

// Set the request
request.Requests = sr;

// Give your application ID
request.AppID = "8D597754B067D715334B61B44FB1921A4DBE5C76";
// Indicate the culture
request.CultureInfo = "en-US";

// Request the service
SearchResponse resp = liveSearchSvc.Search(request);

}

Remark: As you can see in the code above we include our application ID directly into the code, in the "real-life" you could include this ID# into a configuration file.

I think this code is easy enough, no need to give more explanation.

6 - Advanced search

In this part we will send request a bit more complex, with different parameters on the expected result. The Live Search API allows users to setup a lot of parameters and then get very accurate request results.
Here is the commented code of a research using more advanced:

Advanced search

public void AdvancedRequest()
{
// Live Search Service
MSNSearchService liveSearchSvc = new MSNSearchService();

// Request object
SearchRequest request = new SearchRequest();

// 'sr' contains types of sources we are going to request (pages, videos, images...)
SourceRequest[] sr = new SourceRequest[3];

// Request the web pages, with all fields in response
sr[0] = new LiveSearchWS.SourceRequest();
sr[0].Source = LiveSearchWS.SourceType.Web;
sr[0].Count= 50;
sr[0].ResultFields = ResultFieldMask.Url |
ResultFieldMask.Description |
ResultFieldMask.Title;

// Request the images, with all fields in response
sr[1] = new LiveSearchWS.SourceRequest();
sr[1].Source = LiveSearchWS.SourceType.Image;
sr[0].Count = 12;
// Return the image and its URL
sr[1].ResultFields = ResultFieldMask.Image |
ResultFieldMask.Url;

// Request the images, with all fields in response
sr[2] = new LiveSearchWS.SourceRequest();
// Search into the news
sr[2].Source = LiveSearchWS.SourceType.News;
sr[0].Count = 50;
// Return the news url, title, description and source
sr[2].ResultFields = ResultFieldMask.Url |
ResultFieldMask.Title |
ResultFieldMask.Description |
ResultFieldMask.Source;

// The request. Search only on the cnn.com website
request.Query = "Beijing 2008";

// Will return filtered results (moderated level)
request.SafeSearch = SafeSearchOptions.Moderate;
// Will mark the words included in the query text
request.Flags = SearchFlags.MarkQueryWords;

// Set the sources of the request
request.Requests = sr;

// Give your application ID
request.AppID = "8D597754B067D715334B61B44FB1921A4DBE5C76";
// Indicate the culture
request.CultureInfo = "en-US";

// Request the service
SearchResponse resp = liveSearchSvc.Search(request);

}

Important: by default each request return 10 results, the maximum is 50 but not for all the types (like phonebook (25) or images (10)). We can set the number of results to be returned with the Count property of the SourceRequest object, if this number is bigger than the maximum allowed then the service will return only 10 results.

Conclusion

The Live Search API is a complete solution to add search capabilities in a Web Site. Within very few lines it's very easy to request the Microsoft Live Search engine like we can do with the www.live.com page. If you had read this article it means it's probably not a problem for you, but in case you have problem to write C# code Microsoft give you the possibility to integrate Live Search in your website without coding, you just need to include some HTML tags… You will see, even if you are not an IT specialist you will find it rather easy. For more details you can consult this page: http://search.live.com/siteowner. To be totally complete I have to mention that Google offer a similar API for search, you can find more details here http://code.google.com/. I hope this small introduction at Live Search Web Service gave you the motivation to discover further this great API.

Ressources

Liens vers le MSDN

Classe SourceRequest
Classe SerachRequest

Liens vers Developpez.com

Mes articles
Mon blog
Les articles des autres rédacteurs
La rubrique .Net de Developpez.com