Click here to Skip to main content
15,879,326 members
Articles / Desktop Programming / Windows Forms

Bing API in Action - C#

Rate me:
Please Sign up or sign in to vote.
4.61/5 (18 votes)
17 Jul 2009CPOL2 min read 438.5K   34   25
Short article which explains how to use the Bing API in C#.NET.

Image 1

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.

C#
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

C#
using WindowsFormsApplication1.MyBingService;

This is the web reference you added in Step 2.

C#
LiveSearchService service = new LiveSearchService();

I create an object for LiveSearchService.

C#
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.
C#
SearchResponse response = service.Search(request);

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

C#
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)


Written By
Architect ORION INDIA SYSTEMS
India India
Praveen.V.Nair - aka NinethSense - PMP, Microsoft MVP - is working as a Head of Technology and Architecture at Orion India Systems, Kochi, India. 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/.

Comments and Discussions

 
QuestionBing LiveSearchService Pin
Member 105004333-Mar-14 23:03
Member 105004333-Mar-14 23:03 
QuestionI only get 10 results Pin
Nikolas1994i1-Mar-12 2:27
Nikolas1994i1-Mar-12 2:27 
GeneralMy vote of 3 Pin
Hasitha_Dayarathna5-Mar-11 1:58
Hasitha_Dayarathna5-Mar-11 1:58 
Generalfind not liveserv Pin
mehmettemel14-Aug-10 11:16
mehmettemel14-Aug-10 11:16 
GeneralRe: find not liveserv Pin
Member 768565918-Feb-11 7:55
Member 768565918-Feb-11 7:55 
GeneralRe: find not liveserv Pin
Praveen Nair (NinethSense)18-Feb-11 12:03
Praveen Nair (NinethSense)18-Feb-11 12:03 
Generalthe number of results Pin
xxsaxx6-Dec-09 21:39
xxsaxx6-Dec-09 21:39 
GeneralCould not find LiveSearchService() Pin
itsravie22-Sep-09 7:40
itsravie22-Sep-09 7:40 
GeneralRe: Could not find LiveSearchService() Pin
Member 437383917-Dec-09 23:06
Member 437383917-Dec-09 23:06 
GeneralRe: Could not find LiveSearchService() Pin
juidan27-Apr-10 2:02
juidan27-Apr-10 2:02 
GeneralRe: Could not find LiveSearchService() Pin
Garry Lowther21-Nov-11 4:24
Garry Lowther21-Nov-11 4:24 
GeneralResults.. Pin
-Sean-14-Aug-09 14:38
-Sean-14-Aug-09 14:38 
GeneralRe: Results.. Pin
Praveen Nair (NinethSense)14-Aug-09 20:51
Praveen Nair (NinethSense)14-Aug-09 20:51 
GeneralNice Article. Pin
nkmkrishna30-Jul-09 2:32
nkmkrishna30-Jul-09 2:32 
JokeRe: Nice Article. Pin
Praveen Nair (NinethSense)30-Jul-09 3:14
Praveen Nair (NinethSense)30-Jul-09 3:14 
Generalgetting started Pin
senerd0224-Jul-09 23:47
senerd0224-Jul-09 23:47 
GeneralRe: getting started Pin
senerd0225-Jul-09 1:29
senerd0225-Jul-09 1:29 
GeneralRe: getting started Pin
Praveen Nair (NinethSense)26-Jul-09 19:50
Praveen Nair (NinethSense)26-Jul-09 19:50 
GeneralThank you Pin
Svein Erik Storkås23-Jul-09 22:39
Svein Erik Storkås23-Jul-09 22:39 
GeneralDecent article Pin
Blue36520-Jul-09 10:04
Blue36520-Jul-09 10:04 
GeneralLook at the Bing Sharp library Pin
Joseph Guadagno20-Jul-09 4:46
Joseph Guadagno20-Jul-09 4:46 
GeneralRe: Look at the Bing Sharp library Pin
Praveen Nair (NinethSense)20-Jul-09 18:29
Praveen Nair (NinethSense)20-Jul-09 18:29 
GeneralMy vote of 1 Pin
Mike Ozzie20-Jul-09 3:00
Mike Ozzie20-Jul-09 3:00 
GeneralRe: My vote of 1 Pin
Praveen Nair (NinethSense)20-Jul-09 18:29
Praveen Nair (NinethSense)20-Jul-09 18:29 
GeneralRe: My vote of 1 Pin
Praveen Nair (NinethSense)21-Jul-09 19:04
Praveen Nair (NinethSense)21-Jul-09 19:04 

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

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