Consuming the Bing API
You can simply add the Web reference to the BingAPI appended with the AppId. Below are the API Urls available:
- http://api.search.live.net/json.aspx?Appid=<AppId>
- http://api.search.live.net/xml.aspx?Appid=<AppId>
- http://api.search.live.net/soap.aspx?Appid=<AppId>
Add the web reference to the soap.aspx since our demo will be using the SOAP requests. Refer fig 3.0.
[bing3.jpg]
Fig 3.0
Go to the code behind file and implement the SearchButton click event handler to perform the search and to display the results onto the ResultsTable. Below is the code behind of SearchPage.aspx.cs:
using System;
using BingSearchAPIDemo.LiveSearchApi;
using System.Configuration;
using System.Web.UI.HtmlControls;
namespace BingSearchAPIDemo
{
public partial class SearchPage : System.Web.UI.Page
{
private const string APP_ID_KEY = "BingAppId";
private const string ALT_ROW_STYLE_KEY = "AlternateRowStyle";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SearchButton_Click(object sender, EventArgs e)
{
LiveSearchService liveSearchService = new LiveSearchService();
SearchRequest searchRequest = new SearchRequest();
//Set the AppId
searchRequest.AppId = ConfigurationManager.AppSettings[APP_ID_KEY];
//Set the source type
searchRequest.Sources = new SourceType[] { SourceType.Web };
//Set the query with the value entered in the TextBox
searchRequest.Query = SearchTextBox.Text;
//Do the search by passing the SearchRequest object created
SearchResponse searchResponse = liveSearchService.Search(searchRequest);
int index = 1;
//Do a for each
foreach (var webResult in searchResponse.Web.Results)
{
//Bind the result row
BuildResultRow(webResult, index);
index++;
}
}
private void BuildResultRow(WebResult webResult, int index)
{
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell cell = new HtmlTableCell();
HtmlAnchor anchor = new HtmlAnchor();
anchor.InnerText = webResult.Title;
anchor.HRef = webResult.Url;
cell.Controls.Add(anchor);
row.Controls.Add(cell);
if (index % 2 > 0)
row.Attributes.Add("class", ALT_ROW_STYLE_KEY);
ResultsTable.Controls.Add(row);
row = new HtmlTableRow();
cell = new HtmlTableCell();
cell.InnerText = webResult.Description;
row.Controls.Add(cell);
if (index % 2 > 0)
row.Attributes.Add("class", ALT_ROW_STYLE_KEY);
ResultsTable.Controls.Add(row);
}
}
}
There are many source types available, in the above example we have used Web. Some of the other SourceTypes are Image, Video, Weather, etc.
Run the application, enter the SearchText as Kodaikanal and see the result getting bound to the Results table. Below is the output screen shot Fig 3.1.
[bing4.jpg]
Fig 3.1
Performing Asynchronous Search
The API also allows performing searches in an asynchronous manner. In order to do that you have to use the SearchAsync method and use the SearchCompleted event to perform any post search operations like binding the results to the HTML Table.
private void DoSearch()
{
LiveSearchService liveSearchService = new LiveSearchService();
SearchRequest searchRequest = new SearchRequest();
//Set the AppId
searchRequest.AppId = ConfigurationManager.AppSettings[APP_ID_KEY];
//Set the source type
searchRequest.Sources = new SourceType[] { SourceType.Web };
//Set the query with the value entered in the TextBox
searchRequest.Query = SearchTextBox.Text;
//Hook up the SearchCompleted event with an event handler
liveSearchService.SearchCompleted += new SearchCompletedEventHandler(liveSearchService_SearchCompleted);
//Do the search by passing the SearchRequest object created
liveSearchService.SearchAsync(searchRequest);
}
void liveSearchService_SearchCompleted(object sender, SearchCompletedEventArgs e)
{
//Get the result using e.Result.Web
}
Conclusion
Thus in this article I have described with an example how to implement Bing search in your application using the Bing APIs available. The sample application is attached below.
Happy reading! See you in my next article.
Related Articles
Comments
There are no comments yet. Be the first to comment!