Consuming the Live Search API in your Web Applications - Windows Live

Well, I havent talked much about Windows Live and when the whole world is keen about our Live initiative and the exciting bunch of services we have unveiled, I thought of starting it up, with an article on how you can consume the Live Search API and implement Live Search within your web applications.

Before getting started, there are a few things you would need to know about Live.

Windows Live is a platform.  It is a collection of online services which can be utilized as well as programmed against.  To read more about Live from a Developer perspective, visit http://dev.live.com

I want to divide this article into four sections

1. Creating an Application ID for consuming the Live Service

2. Implementing the Live Search in your Web Application

3. Tip / trick on overcoming against proxies in the above sample

4. Resources for getting started with similar APIs.

Creating an Application ID for consuming the Live Service

Before you could start using the Live Search API, you need an Application ID for being able to query using the Service.  To get the same, visit http://search.msn.com/developer.

Sign in using your passport account (if you have a hotmail / msn.com account its already a passport) so that you can create / manage Application IDs.  If you are using it for the first time, you would have the option to only Create Application IDs.  Specify an Application name and you would be provided with an Application ID which you  must be using in your sample in the Section 2 of this article.  There after you can login to this site and Edit / Delete and manage your Application ID settings.

Implementing the Live Search in your Web Application

Once you have created the Application ID, you are all set for programming the Live Search API and getting results.  For my sample, I have used a simple textbox, Button, Label and a Gridview for displaying results.  Let us see how the ASPX File looks like

<form id="form1" runat="server" defaultbutton="Button1">
    <div>
        <asp:TextBox ID="TextBox1" runat="Server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="Server" Text="Search" OnClick="Button1_Click" />
        <br />
        <hr />
        <asp:Label ID="Label1" runat="Server"></asp:Label>
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="Server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false">
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#EFF3FB" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:BoundField DataField="Title" HeaderText="Title" ControlStyle-Width="30%" />
                <asp:BoundField DataField="Description" HeaderText="Description" ControlStyle-Width="60%" />
                <asp:HyperLinkField DataTextField="Url" HeaderText="URL" ControlStyle-Width="20%" DataNavigateUrlFields="URL" />
            </Columns>
        </asp:GridView>
    </div>
    </form>

In this sample, I am binding the Title, Description as well as the URL Fields.  But there are more fields which you may want to bind to.  You can keep adding BoundFields to the above Columns collection of the GridView to add more fields.

The Next step is to consume the Live Search Web Service.

The WSDL for the Search Service is http://soap.search.msn.com/webservices.asmx?wsdl.

Add Web Reference to your Website with the above WSDL.  The default Web Reference is com.msn.search.soap, but you can change it as per your wish.  However, this name should be referred in the Code behind file under the name space declarations.

Once you are done adding the web reference, go to the above ASPX Page - design view and double click on the "Search" button to generate the Button Click event.

Go to the Code behind and in the name space declarations, add the following namespaces:-

using System.Web.Services.Protocols;
using com.msn.search.soap;
using System.Net;

Go to the Button Click event and add the following code:-

 try
        {
            MSNSearchService s = new MSNSearchService();
            SearchRequest searchRequest = new SearchRequest();
            int arraySize = 1;
            SourceRequest[] sr = new SourceRequest[arraySize];

            sr[0] = new SourceRequest();
            sr[0].Source = SourceType.Web;

            searchRequest.Query = TextBox1.Text;
            searchRequest.Requests = sr;
 
           searchRequest.AppID = "APP ID you generated from http://search.msn.com/developer";
            searchRequest.CultureInfo = "en-US";
            SearchResponse searchResponse;

            searchResponse = s.Search(searchRequest);

            foreach (SourceResponse sourceResponse in searchResponse.Responses)
            {
                Result[] sourceResults = sourceResponse.Results;
                if (sourceResponse.Total > 0)
                {
                    Label1.Text = sourceResponse.Source.ToString() + " - Total Results: " + sourceResponse.Total.ToString();
                }
                GridView1.DataSource = sourceResults;
                GridView1.DataBind();
        }
        catch (SoapException ex)
        {
            Response.Write(ex.Message);
        }
        catch (WebException ex)
        {
            Response.Write(ex.ToString());
        }

In the above sample,  replace the App ID comment with the actual Application ID you generated out of http://search.msn.com/developer

Tip / Trick on overcoming against proxies

Your sample is ready to run.  However, in case you are behind proxy / firewall, there might be a System.Net Exception error you get at catch.  To solve that, use the following settings in the Web.Config.  You need to place the following settings above <system.web> start tag or after the </system.web> end tag, basically outside the system.web settings.

<system.net>
    <defaultProxy>
      <proxy usesystemdefault="False"
             proxyaddress="Specify Your Proxy Address: Specify Port Number"
bypassonlocal="False"/>
    </defaultProxy>
  </system.net>

Earlier, I had written a detailed article on this topic, as well as how you can specify the above settings programmatically.  Check http://geekswithblogs.net/ranganh/archive/2005/08/29/51474.aspx for the same.

When you now run your Website, you can see that upon entering a search item in the TextBox and clicking on the "Search" button, you get the search results from Live.com bound to your GridView

Resources

To learn more about Live visit http://www.live.com

To learn more about the Developer services visit http://dev.live.com

For more samples on the other APIs, visit http://msdn2.microsoft.com/en-us/library/bb264574.aspx

Cheers!!!

Print | posted on Monday, May 28, 2007 6:51 AM

Comments on this post

No comments posted yet.

Your comment:

 (will show your gravatar)