how to use the Bing API in C#.NET

No.of Views1563
Bookmarked0 times
Downloads 
Votes0
By  ninethsense   On  19 Jun 2010 21:06:56
Tag : CSharp , Miscellaneous
Short article which explains how to use the Bing API in C#.NET.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

According to Bing, "Bing is a search engine that finds and organizes the answers you need so you can make faster, more informed decisions". Microsoft made available its API to public so that we can use it from our own applications. This article explains how to make a sample application which uses a Bing search feature. 

Image Loading

Step 1

Even though the Bing API service is free, you need an application ID to use the service. You can create it from here: http://www.bing.com/developers/createapp.aspx.

An AppID looks like this: F2C2567D3A712A4E764C49473BF4AFF7F8722A4E.

Note: Do not use this as this is a fake AppID. Remember that you will need to sign in using your Windows Live ID.

Step 2

Now, you can create a Windows project. (I used Visual C# 2008 Express.) You need to add a web reference to: http://api.search.live.net/search.wsdl?AppID=YourAppId.

Use your AppId for YourAppId. I gave the name MyBingService. You may give any name of your choice.

Tip: You can add a Web Reference by right clicking the Project node in Solution Explorer -> Add Service Reference -> Advanced... button -> Add Web Reference... button.

Step 3

Below is the sample code. Note that I used only the necessary code/properties for the demonstration. You may look at the Bing API documentation for complete information.

using System;
using System.Windows.Forms;
 
using WindowsFormsApplication1.MyBingService;
 
namespace WindowsFormsApplication1
{public partial class Form1 : Form
    {public Form1()
        {
            InitializeComponent();
        }private void Form1_Load(object sender, EventArgs e)
        {
            LiveSearchService service = new LiveSearchService();
 
            SearchRequest request = new SearchRequest();// use your Bing AppIDrequest.AppId = "put_your_AppID_her_please";
            request.Query = "ninethsense"; // your search query// I want to search only webrequest.Sources = new SourceType[] { SourceType.Web }; 
 
 
            SearchResponse response = service.Search(request);foreach (WebResult result in response.Web.Results)
            {
                listBox1.Items.Add(result.Title + " URL: " + result.Url);
            }
        }
    }
}

 

Source code explanation

using WindowsFormsApplication1.MyBingService;

This is the web reference you added in Step 2.

LiveSearchService service = new LiveSearchService();

 
I create an object for LiveSearchService.

SearchRequest request = new SearchRequest();
request.AppId = "put_your_AppID_her_please"; // use your Bing AppIDrequest.Query = "ninethsense"; // your search query// I want to search only webrequest.Sources = new SourceType[] { SourceType.Web }; 

I create a search query (request.Query) using the AppID (request.AppId) and specify whether I have to search web or image or video or...
Refer to the documentation for all available source types. Some are:

  • SourceType.Web
  • SourceType.Image
  • SourceType.Video
  • SourceType.Spell
  • SourceType.News
  • SourceTyp3.InstantAnswer
  • etc.
SearchResponse response = service.Search(request);

 Do search! Result(s) will be available in the identifier response.

foreach (WebResult result in response.Web.Results)
{
    listBox1.Items.Add(result.Title + " URL: " + result.Url);
}

 I placed a ListBox control in the form to show search data. This code populates the ListBox.

Step 4

No step 4! Just execute the application.

Why no source code is attached?

Simply because I do not like to ship with my personal AppId :). Also, I was too lazy to write a custom configuration file to store that - which may make the article complex for a beginner. Ask me questions! I am here.

Useful links

  • Bing Developer Center - http://www.bing.com/developers/
  • Create AppId - http://www.bing.com/developers/createapp.aspx
  • Bing API Version 2.0 - http://msdn.microsoft.com/en-us/library/dd251056.aspx
  • Bing FAQ - http://help.live.com/help.aspx?mkt=en-us&project=searchdevctr&querytype=keyword&query=faq

That's all enjoy search with Bing in .NET.

 
Sign Up to vote for this article
 
About Author
 
ninethsense
Occupation-
Company-
Member Type-Junior
Location-Not Provided
Joined date-21 Jul 2009
Home Page-http://blog.ninethsense.com/
Blog Page-http://blog.ninethsense.com/
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top