What is the Bing Search API?
The Bing Application Programming Interface (API) enables developers to programmatically submit queries to and retrieve results from the Bing Search Engine. Bing also introduces a superior service for developers. It encourages developers to use their Search API into their own application.
Here, I am going to demonstrate you How to Use Bing Search API in our application.
I’ll discuss about to Integrate Bing Search API from very basics (from registering on
Windows Azure Marketplace to Using .Net Class Library to the application).
To use Bing Search API, firstly you need to register on Windows Azure Marketplace.
When you will click on Sign in Link, it will redirect you to Windows Live Id
Authentication Page; here provide your Windows Live ID and Password to
Continue. Once successfully Logged In, you will further see the Windows Azure
Market Place
Registration Page as shown below.
After providing the required details Click Continue.
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.
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.
In My Account Section user can see about their Account information, List of
Applications, Account Data and Account Key.
From the My Account section, you can manage your Subscriptions and Account Keys.
Note: 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.
Till now, we have successfully registered with the Windows Azure Marketplace; also
we have gone through the different and important section of the user account.
Now, the next step is to Subscribe for the Bing Search API. Bellow is the direct link
to Subscribe Bing Search API.
https://datamarket.azure.com/dataset/5ba839f1-12ce-4cce-bf57-a49d98d29a44
By following the above link you will see the following window.
The above page provides options to Subscribe for your API on the basis of your
requirements (Paid/Free). Here, in my example I am going to use Free Subscription
Option.
To Subscribe the API Click on Sign Up Button.
After Clicking on SIGN UP Button, following window will appear. It will show you
Terms of Use of the Bing Search Api.
Read the Terms of Use, Select I Agree Checkbox and Click on SIGN UP Button to
move to next step.
Following screen will be in front of you, which is a Thank You page for subscribing
the API.
Don’t forget to notice, I have marked something with Red Circle in Right Upper
Side of the Page. This is your Bing Search API Class Library File (.Net C# Class
Library), which will be used in program to make it enable to use Bing Search API.
Download and Save the .Net C# Class Library File by simply clicking on it.
Till Now you have finished all the steps that are needs to be followed for
Registering on Windows Azure Marketplace, Subscribing Bing Search API and
Downloading the .Net C# Class Library File. Now we are ready to use it in our
program.
1. Open Visual Studio 2010 >File >New >Website >Under Template Section Choose ASP.NET Empty Web Site, Choose Location to save it and Provide name to it. Click OK.
2. Now, you have to add the Downloaded .Net C# Class Library to your Project. You can do this task by Right Clicking on Solution Explorer > Add Existing Item >Browse the Location of File > Add it in your Project.
Here is a Screenshots of the Project Solution Explorer after adding the .Net C# Class Library file to the project.
It will be better if we add the File to the App_Code Folder as follows.
3. To build the code file you need to add reference to the following library.
System.Data.Services.Client
You can add the reference by Right Clicking on Solution Explorer >Add Reference > Select .Net Tab.
4. Now, add an ASP WEB Form to your project by Right Clicking on Solution Explorer >Add New Item.
Create the Following UI for the Web Page you have added.
In above UI, I have taken a TextBox Control for Entering Search Term into it, a
Button Control and a GridView Control to show the search result.
In GridView I have added three Template Control i.e., a Label Control for Displaying
the Title, a Hyperlink Control which will enable user to Navigate to the Page and
again a Label Control to show the description of the site. I have bind all these
Controls to the Data Source using
'<%# Bind("<ColumnName>") %>'
Here is the code snippet that I have written for the Default.aspx page.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width:100%;">
<tr>
<td align="center" bgcolor="#99CCFF" colspan="2">
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Names="Georgia"
Text="Integrate Bing Search API into ASP.Net application"></asp:Label>
</td>
</tr>
<tr>
<td width="80px">
</td>
<td height="40px">
<asp:TextBox ID="txtSearch" runat="server" Height="30px" Width="639px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Font-Bold="True" Font-Names="Georgia"
Height="30px" Text="Search" Width="65px" onclick="Button1_Click" />
</td>
</tr>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td style="border-color: #999999"> </td>
<td style="border-color: #999999">
<asp:GridView ID="GridViewSearchResult" runat="server"
AutoGenerateColumns="false" ShowHeader="false" Width="712px"
BorderColor="White">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblTitle" runat="server" Text='<%# Bind("Title") %>'
Font-Size="Large"></asp:Label>
<br />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#Bind("Url") %>'><%#Eval("Url") %></asp:HyperLink>
<br />
<asp:Label ID="lblDescription" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
<br />
<br />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
5. Now, Switch to Default.aspx.cs file.
Add Following Code for Button Click Event Handler.
//Add the namespace "Using Bing;"
protected void Button1_Click(object sender, EventArgs e)
{
var objBing = new Bing.BingSearchContainer(new Uri("https://api.datamarket.azure.com/Bing/Search"));
//Replace this Account Key with you Account Key
var accountKey = "EMdPzpSl/qTbvz6/ScfE5sFLzuxNfU/crNSmIYjQDes=";
//Passing the Credintial
objBing.Credentials = new System.Net.NetworkCredential(accountKey, accountKey);
//Following Line is used to get the Search Result as DataSource.
var webResult = objBing.Web(txtSearch.Text.ToString(),null,null,null,null,null,null,null);
//Binding the Resultant DataSource to the GridView
GridViewSearchResult.DataSource = webResult;
GridViewSearchResult.DataBind();
}
Enter the Search Term into the TextBox and Click on Search Button to get the Result.
Result will be shown as follows:
In this article I have described everything in very simple manner, also tried to go through the every terms and concept for Integrating Bing Search Api in our application. Screenshots for every step are provided to make is simple to be go through the every terms and concept for Integrating Bing Search Api in our application. Screenshots for every step are provided to make is simple to be used. Using these steps you can easily Integrate Bing Search API you can easily give a useful functionality for Searching Stuffs in your application. I hope this will be a good article for you.
Leave Comment