Integrate Bing Search API into ASP.Net application

Posted by sreejukg on ASP.net Weblogs See other posts from ASP.net Weblogs or by sreejukg
Published on Wed, 04 Jul 2012 08:28:00 GMT Indexed on 2012/07/04 9:16 UTC
Read the original article Hit count: 926

Couple of months back, I wrote an article about how to integrate Bing Search engine (API 2.0) with ASP.Net website. You can refer the article here

http://weblogs.asp.net/sreejukg/archive/2012/04/07/integrate-bing-api-for-search-inside-asp-net-web-application.aspx

Things are changing rapidly in the tech world and Bing has also changed! The Bing Search API 2.0 will work until August 1, 2012, after that it will not return results. Shocked? Don’t worry the API has moved to Windows Azure market place and available for you to sign up and continue using it and there is a free version available based on your usage.

In this article, I am going to explain how you can integrate the new Bing API that is available in the Windows Azure market place with your website.

You can access the Windows Azure market place from the below link

https://datamarket.azure.com/

There is lot of applications available for you to subscribe and use. Bing is one of them. You can find the new Bing Search API from the below link

https://datamarket.azure.com/dataset/5BA839F1-12CE-4CCE-BF57-A49D98D29A44

To get access to Bing Search API, first you need to register an account with Windows Azure market place. Sign in to the Windows Azure market place site using your windows live account. Once you sign in with your windows live account, you need to register to Windows Azure Market place account.

From the Windows Azure market place, you will see the sign in button it the top right of the page.

clip_image002

Clicking on the sign in button will take you to the Windows live ID authentication page. You can enter a windows live ID here to login. Once logged in you will see the Registration page for the Windows Azure market place as follows.

clip_image004

You can agree or disagree for the email address usage by Microsoft. I believe selecting the check box means you will get email about what is happening in Windows Azure market place. Click on continue button once you are done.

In the next page, you should accept the terms of use, it is not optional, you must agree to terms and conditions. Scroll down to the page and select the I agree checkbox and click on Register Button.

clip_image006

Now you are a registered member of Windows Azure market place. You can subscribe to data applications. In order to use BING API in your application, you must obtain your account Key, in the previous version of Bing you were required an API key, the current version uses Account Key instead.

Once you logged in to the Windows Azure market place, you can see “My Account” in the top menu, from the Top menu; go to “My Account” Section.

clip_image008

From the My Account section, you can manage your subscriptions and Account Keys. Account Keys will be used by your applications to access the subscriptions from the market place.

Click on My Account link, you can see Account Keys in the left menu and then Add an account key or you can use the default Account key available.

clip_image010

Creating account key is very simple process. Also you can remove the account keys you create if necessary.

The next step is to subscribe to BING Search API. At this moment, Bing Offers 2 APIs for search. The available options are as follows.

1. Bing Search API - https://datamarket.azure.com/dataset/5ba839f1-12ce-4cce-bf57-a49d98d29a44

2. Bing Search API – Web Results only - https://datamarket.azure.com/dataset/8818f55e-2fe5-4ce3-a617-0b8ba8419f65

The difference is that the later will give you only web results where the other you can specify the source type such as image, video, web, news etc. Carefully choose the API based on your application requirements. In this article, I am going to use Web Results Only API, but the steps will be similar to both.

Go to the API page https://datamarket.azure.com/dataset/8818f55e-2fe5-4ce3-a617-0b8ba8419f65, you can see the subscription options in the right side. And in the bottom of the page you can see the free option

clip_image011

Since I am going to use the free options, just Click the Sign Up link for that.

clip_image013

Just select I agree check box and click on the Sign Up button. You will get a recipt pagethat detail your subscription.

clip_image015

Now you are ready Bing Search API – Web results. The next step is to integrate the API into your ASP.Net application. Now if you go to the Search API page (as well as in the Receipt page), you can see a .Net C# Class Library link, click on the link, you will get a code file named “BingSearchContainer.cs”.

In the following sections I am going to demonstrate the use of Bing Search API from an ASP.Net application.

Create an empty ASP.Net web application. In the solution explorer, the application will looks as follows.

clip_image016

Now add the downloaded code file (“BingSearchContainer.cs”) to the project. Right click your project in solution explorer, Add -> existing item, then browse to the downloaded location, select the “BingSearchContainer.cs” file and add it to the project.

To build the code file you need to add reference to the following library.
System.Data.Services.Client

You can find the library in the .Net tab, when you select Add -> Reference

clip_image017

Try to build your project now; it should build without any errors.

Add an ASP.Net page to the project. I have included a text box and a button, then a Grid View to the page. The idea is to Search the text entered and display the results in the gridview. The page will look in the Visual Studio Designer as follows.

clip_image018

The markup of the page is as follows.

clip_image020

In the button click event handler for the search button, I have used the following code.

clip_image022

Now run your project and enter some text in the text box and click the Search button, you will see the results coming from Bing, cool.

I entered the text “Microsoft” in the textbox and clicked on the button and I got the following results.

clip_image024

Searching Specific Websites

If you want to search a particular website, you pass the site url with site:<site url name> and if you have more sites, use pipe (|).

e.g. The following search query

site:microsoft.com | site:adobe.com design

will search the word design and return the results from Microsoft.com and Adobe.com

See the sample code that search only Microsoft.com for the text entered for the above sample.

var webResults = bingContainer.Web("site:www.Microsoft.com " + txtSearch.Text, null, null, null, null, null, null);

Paging the results returned by the API

By default the BING API will return 100 results based on your query. The default code file that you downloaded from BING doesn’t include any option for this. You can modify the downloaded code to perform this paging.

The BING API supports two parameters $top (for number of results to return) and $skip (for number of records to skip). So if you want 3rd page of results with page size = 10, you need to pass $top = 10 and $skip=20.

Open the BingSearchContainer.cs in the editor. You can see the Web method in it as follows.

public DataServiceQuery<WebResult> Web(String Query, String Market, String Adult, Double? Latitude, Double? Longitude, String WebFileType, String Options) { 

In the method signature, I have added two more parameters

public DataServiceQuery<WebResult> Web(String Query, String Market, String Adult, Double? Latitude, Double? Longitude, String WebFileType, String Options, int resultCount, int pageNo) {

and in the method, you need to pass the parameters to the query variable.

query = query.AddQueryOption("$top", resultCount);
query = query.AddQueryOption("$skip", (pageNo -1)*resultCount);
return query;

Note that I didn’t perform any validation, but you need to check conditions such as resultCount and pageCount should be greater than or equal to 1. If the parameters are not valid, the Bing Search API will throw the error.

The modified method is as follows.

image

The changes are highlighted. Now see the following code in the SearchPage.aspx.cs file

protected void btnSearch_Click(object sender, EventArgs e)
{
    var bingContainer = new Bing.BingSearchContainer(new Uri(https://api.datamarket.azure.com/Bing/SearchWeb/));
    // replace this value with your account key
    var accountKey = "your key";
    // the next line configures the bingContainer to use your credentials.
    bingContainer.Credentials = new NetworkCredential(accountKey, accountKey);
    var webResults = bingContainer.Web("site:microsoft.com" +txtSearch.Text , null, null, null, null, null, null,3,2);
    lstResults.DataSource = webResults;
    lstResults.DataBind();
}

The following code will return 3 results starting from second page (by skipping first 3 results). See the result page as follows.

clip_image028

Bing provides complete integration to its offerings. When you develop search based applications, you can use the power of Bing to perform the search. Integrating Bing Search API to ASP.Net application is a simple process and without investing much time, you can develop a good search based application.

Make sure you read the terms of use before designing the application and decide which API usage is suitable for you.

Further readings

BING API Migration Guide

http://go.microsoft.com/fwlink/?LinkID=248077

Bing API FAQ

http://go.microsoft.com/fwlink/?LinkID=252146

Bing API Schema Guide

http://go.microsoft.com/fwlink/?LinkID=252151

© ASP.net Weblogs or respective owner

Related posts about ASP.NET

Related posts about ASP.NET 4