"Knowledge has to be improved, challenged, and increased constantly, or it vanishes."

Integrate BING API for Search inside ASP.Net web application

Update:  Since Bing has moved Search API to Windows Azure Market place, I wrote a new article about the new Bing Search API. You can read this from the below link

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

As you might already know, Bing is the Microsoft Search engine and is getting popular day by day. Bing offers APIs that can be integrated into your website to increase your website functionality. At this moment, there are two important APIs available. They are

  • Bing Search API
  • Bing Maps

The Search API enables you to build applications that utilize Bing’s technology. The API allows you to search multiple source types such as web; images, video etc. and supports various output prototypes such as JSON, XML, and SOAP. Also you will be able to customize the search results as you wish for your public facing website.

Bing Maps API allows you to build robust applications that use Bing Maps.

In this article I am going to describe, how you can integrate Bing search into your website. In order to start using Bing, First you need to sign in to http://www.bing.com/toolbox/bingdeveloper/ using your windows live credentials.

clip_image001

Click on the Sign in button, you will be asked to enter your windows live credentials. Once signed in you will be redirected to the Developer page. Here you can create applications and get AppID for each application. Since I am a first time user, I don’t have any applications added.

clip_image003

Click on the Add button to add a new application. You will be asked to enter certain details about your application. The fields are straight forward, only thing you need to note is the website field, here you need to enter the website address from where you are going to use this application, and this field is optional too. Of course you need to agree on the terms and conditions and then click Save.

clip_image005

clip_image007

Once you click on save, the application will be created and application ID will be available for your use.

clip_image008

Now we got the APP Id. Basically Bing supports three protocols. They are JSON, XML and SOAP. JSON is useful if you want to call the search requests directly from the browser and use JavaScript to parse the results, thus JSON is the favorite choice for AJAX application. XML is the alternative for applications that does not support SOAP, e.g. flash/ Silverlight etc. SOAP is ideal for strongly typed languages and gives a request/response object model. In this article I am going to demonstrate how to search BING API using SOAP protocol from an ASP.Net application.

For the purpose of this demonstration, I am going to create an ASP.Net project and implement the search functionality in an aspx page.

Open Visual Studio, navigate to File-> New Project, select ASP.Net empty web application, I named the project as “BingSearchSample”.

clip_image010

Add a Search.aspx page to the project, once added the solution explorer will looks similar to the following.

clip_image011

Now you need to add a web reference to the SOAP service available from Bing. To do this, from the solution explorer, right click your project, select Add Service Reference.

clip_image012

Now the new service reference dialog will appear. In the left bottom of the dialog, you can find advanced button, click on it.

clip_image013

Now the service reference settings dialog will appear. In the bottom left, you can find Add Web Reference button, click on it.

clip_image014

The add web reference dialog will appear now. Enter the URL as http://api.bing.net/search.wsdl?AppID=<YourAppIDHere>&version=2.2 (replace <yourAppIDHere> with the appID you have generated previously) and click on the clip_image015 button next to it. This will find the web service methods available. You can change the namespace suggested by Bing, but for the purpose of this demonstration I have accepted all the default settings. Click on the Add reference button once you are done.

clip_image017

Now the web reference to Search service will be added your project. You can find this under solution explorer of your project.

clip_image018

Now in the Search.aspx, that you previously created, place one textbox, button and a grid view. For the purpose of this demonstration, I have given the identifiers (ID) as txtSearch, btnSearch, gvSearch respectively. The idea is to search the text entered in the text box using Bing service and show the results in the grid view. In the design view, the search.aspx looks as follows.

clip_image019

In the search.aspx.cs page, add a using statement that points to net.bing.api.

clip_image020

I have added the following code for button click event handler.

image

The code is very straight forward. It just calls the service with your AppID, a query to search and a source for searching. Let us run this page and see the output when I enter Microsoft in my textbox.

clip_image023

If you want to search a specific site, you can include the site name in the query parameter. For e.g. the following query will search the word Microsoft from www.microsoft.com website.

searchRequest.Query = “site:www.microsoft.com Microsoft”;

The output of this query is as follows.

clip_image025

Integrating BING search API to your website is easy and there is no limit on the customization of the interface you can do. There is no Bing branding required so I believe this is a great option for web developers when they plan for site search.

3 Comments

Comments have been disabled for this content.