Click here to Skip to main content
Licence CPOL
First Posted 17 Jul 2009
Views 38,344
Bookmarked 29 times

Bing API in Action - C#

By | 17 Jul 2009 | Article
Short article which explains how to use the Bing API in C#.NET.

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.

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 AppID
            request.AppId = "put_your_AppID_her_please";
            request.Query = "ninethsense"; // your search query
 
            // I want to search only web
            request.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 AppID
request.Query = "ninethsense"; // your search query

// I want to search only web
request.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

History

  • 18th July, 2009: Initial post.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

NinethSense



India India

Member

Follow on Twitter Follow on Twitter
Praveen.V.Nair - aka NinethSense - Microsoft MVP - is a person with an abnormal passion for technology. He has been playing with electronics from the age of 10 and with computers from the age of 14. He usually blogs at http://blog.ninethsense.com/.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionI only get 10 results PinmemberNikolas1994i2:27 1 Mar '12  
GeneralMy vote of 3 PinmemberHasitha_Dayarathna1:58 5 Mar '11  
Generalfind not liveserv Pinmembermehmettemel11:16 14 Aug '10  
GeneralRe: find not liveserv PinmemberMember 76856597:55 18 Feb '11  
GeneralRe: find not liveserv PinmemberNinethSense12:03 18 Feb '11  
Generalthe number of results Pinmemberxxsaxx21:39 6 Dec '09  
GeneralCould not find LiveSearchService() PinmemberRavi@UK7:40 22 Sep '09  
GeneralRe: Could not find LiveSearchService() PinmemberMember 437383923:06 17 Dec '09  
GeneralRe: Could not find LiveSearchService() Pinmemberjuidan2:02 27 Apr '10  
GeneralRe: Could not find LiveSearchService() PinmemberGarry Lowther4:24 21 Nov '11  
GeneralResults.. Pinmember-Sean-14:38 14 Aug '09  
GeneralRe: Results.. PinmemberNinethSense20:51 14 Aug '09  
GeneralNice Article. Pinmembernkmkrishna2:32 30 Jul '09  
JokeRe: Nice Article. PinmemberNinethSense3:14 30 Jul '09  
Generalgetting started Pinmembersenerd0223:47 24 Jul '09  
GeneralRe: getting started Pinmembersenerd021:29 25 Jul '09  
GeneralRe: getting started PinmemberNinethSense19:50 26 Jul '09  
GeneralThank you PinmemberSvein Erik Storkås22:39 23 Jul '09  
GeneralDecent article PinmemberBlue36510:04 20 Jul '09  
GeneralLook at the Bing Sharp library PinmemberJoseph Guadagno4:46 20 Jul '09  
GeneralRe: Look at the Bing Sharp library PinmemberNinethSense18:29 20 Jul '09  
GeneralMy vote of 1 PinmemberMike Ozzie3:00 20 Jul '09  
GeneralRe: My vote of 1 PinmemberNinethSense18:29 20 Jul '09  
GeneralRe: My vote of 1 PinmemberNinethSense19:04 21 Jul '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 18 Jul 2009
Article Copyright 2009 by NinethSense
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid