September 2, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Tapping Into Microsoft Live Services with C#

  • March 30, 2009
  • By Jani Järvinen
  • Send Email »
  • More Articles »

Chances are that you have already used Microsoft Live services from a consumer standpoint. For instance, if you have used Microsoft Live Search instead of Google, or created a free e-mail account with Hotmail or Windows Live Mail, then you have used some of the services. These useful services are however only two examples of what is available.

In fact, there are about 15 more services in the Live offering. These services allow you to share your files, photos and contacts, synchronize them automatically between different devices, and more. Although many of these services are consumer-oriented, this doesn't mean that they wouldn't be useful in corporations and enterprises. Since the services are by definition open to everyone, the same solutions can be used by both consumers and businesses alike.

Developers on the other hand are interested in the programmable nature of these services. In addition to a regular, interactive web browser interface, most of the services can be accessed from application code. Microsoft supports using especially REST based endpoints to the Live services, but depending on the nature of the service, an XML web service interface (a SOAP interface) might also be supported.

In addition to supporting access from the code level, Microsoft has developed a set of installable tools and components that plug into the Visual Studio IDE. By installing the Live Tools for Visual Studio, you get a set of new components in the tool palette as shown in Figure 1. As an example, with these components, adding an interactive map to your ASP.NET web application is a breeze.


Figure 1. The Live Services tools installed in Visual Studio 2008.

The Live services that Microsoft offers can be divided roughly into five categories as shown in Figure 2. The identity service supports authentication, knowing who is who and which devices belong to whom. Directory service, then, maintains relationships between identities, for example to form a group of people to share information. Storage services allow people to save their data into the cloud (which can also be called a mesh). Communications and presence let people send messages to each other and know who is online or not. Finally, the search and location (or geospatial) services support finding information from the web and showing interactive maps inside your application.


Figure 2. The five Live Services groups.

Next, you are going to see three examples of different Live Services being used from C#.

Searching the Web

To get started with development using the Live services, you can start from the most commonly used offerings. You might have previously used the Live search engine (which, by the way, rumors indicate will be re-branded as Kumo) and the Virtual Earth interactive maps on the Live web site. Both these services apply to a broad range of users, but you can also benefit from these services in your own applications.

For instance, you might want to include the ability to search the web directly from your application, for instance to help in the research of a particular topic. With this in mind, I'll show you how the Live Search can be used from your own applications. With a SOAP interface, it actually doesn't matter whether your application is a GUI, web-based, console or a service application.

To get started using the Live Search from your code, you need to register as a developer and then get your personal (or corporate, as you prefer) application ID or AppID for short. This AppID string works as an identifier when you access the SOAP interface; no other authentication is needed. To register for the service, you would first need to go to the Live Search Developer Center (follow the link "Create an AppID").

To access the SOAP interface, you need to add a service reference to your Visual Studio project. The correct address for the WSDL document is:

http://api.search.live.net/search.wsdl?AppID=NNN...

Here, you would need to replace NNN... with the AppID you created earlier. Once you have added the service reference to your project (it might be a good idea to rename it to something like “LiveSearchServiceReference” instead of just “Service1”), you can start accessing the interface. To do so, you need to prepare a request object of type SearchRequest and then launch the query. The Request object is defined as part of the SOAP interface. For instance, to search for a given string, you would use code similar to this:

const string AppId = "NNN...";
 
LiveSearchPortTypeClient client =
    new LiveSearchPortTypeClient();
LiveSearchServiceReference.SearchRequest request =
    new SearchRequest();
request.AppId = AppId;
request.Query = "c# development developer.com";
request.Sources = new SourceType[] { SourceType.Web };
request.Version = "2.0";
request.Market = "en-us";
request.Web = new WebRequest();
request.Web.Count = 5;
request.Web.CountSpecified = true;
request.Web.Offset = 0;
request.Web.OffsetSpecified = true;
 
SearchResponse response = client.Search(request);

Here, the SOAP client object is first constructed, after which the process is repeated for the SearchRequest object. To search for web content, you need to specify the AppId, your search keywords (the Query property) and select the correct SoruceType with the Sources property. Live Search supports different sources like the web, images, weather information, and even a spell checker (it works with single words only, though).

To execute the search request, you need to call the SOAP interface's Search method. This returns a SearchResponse object which can be thought of as a collection to hits that the search returns. In the example code above, the Web.Count property is used to specify that the first five hits should be returned. The process the results, you could use code like this:

StringBuilder builder = new StringBuilder();
foreach (WebResult result in response.Web.Results)
{
  builder.AppendLine(result.Title);
  builder.AppendLine(result.Description);
  builder.AppendLine(result.Url);
  builder.Append("Last crawled: ");
  builder.AppendLine(result.DateTime);
  builder.AppendLine();
}
MessageBox.Show(builder.ToString());

The Response object contains a collection property called Web.Results which is easy to enumerate through using a foreach construct. Here, the results are simply collected into a StringBuilder object line by line. Once completed, the results are shown in a message box as illustrated in Figure 3.


Figure 3. Results of a Live web search.





Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel