Tuesday, April 19, 2011

Search Bing Through Silverlight C#

Search Bing with silverlight.   Download The Code

First step. Head over to http://www.bing.com/developers/ to get your free api key. It only takes a second and the app will not work without it.
This is for the API version 2.0.  The latest version as of this writing.  If you try other samples out there calling the old service api you will just end  up pulling your hair out like I did.   Microsoft is not issuing any new keys for the old live api search so most of the code examples on the web are worthless.

I'm short of time so I am just going to post the working project and briefly go over the concepts. You will have access to the documentation once you get your key.  BEWARE. Much of the documentation will not work with silverlight.  The examples I saw from Microsoft were JSON or REST.  They forgot to go over the best method, SOAP.  This project uses SOAP to simplify things.

General concept is as follows.
Create a silverlight with asp.net application.

Create a service reference to the Bing API.  http://api.bing.net/search.wsdl
Add a textbox, a button and a datagrid.  The textbox will hold the search term, the button calls the service and the grid displays the results.

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CSSearchBing.BingSearch; //This is the service to bing.
namespace CSSearchBing
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
//construct the call
            BingSearch.SearchRequest request = new BingSearch.SearchRequest();
 // Enter you api key.   Should be put in config file for security reasons. 
          request.AppId = "CC37A72C372AA66EA3BEB42292DDDDDDDDD";

          request.Version="2.2";
          request.Adult = AdultOption.Off;
          request.AdultSpecified = true;
          request.Query = txtSearch.Text; // search for what is in the textbox
          request.Sources = new BingSearch.SourceType[] { BingSearch.SourceType.Web };
          request.Web =new BingSearch.WebRequest();
         request.Web.Count = 10; //Ten results.  Read the documentation on how to get more.
          request.Web.CountSpecified = true;
          BingSearch.BingPortTypeClient client = new BingSearch.BingPortTypeClient("BingPort"); // BingPort is needed as is. Endpoint
          client.SearchCompleted += new EventHandler<SearchCompletedEventArgs>(client_SearchCompleted);
          client.SearchAsync(request);
        }
// our search complete handler.  Required for silverlight.
        void client_SearchCompleted(object sender, SearchCompletedEventArgs e)
        {
            SearchResponse response = e.Result;
            if (response.Web.Results.Count() > 0)
            {
                var results = from result in response.Web.Results
                              select new SearchResult
                              {
                                  SearchResultTitle = result.Title,
                                  SearchResultUri = result.Url,
                                  SearchResultDescription = result.Description
                              };

                dataGrid1.ItemsSource = results.ToList();
            }
        }
// class holds result values to pass on
        public class SearchResult
        {
            public string SearchResultTitle { get; set; }
            public string SearchResultDescription { get; set; }
            public string SearchResultUri { get; set; }
        }
  
  
    }
  
    }










2 comments:

  1. Hi, I found this very interesting..
    I tried to open the file and it says


    C:\Users\Wilson\Downloads\CSSearchBing.Web\CSSearchBing\CSSearchBing.csproj(131,3): The imported project "C:\Program Files (x86)\MSBuild\Microsoft\Silverlight\v4.0\Microsoft.Silverlight.CSharp.targets" was not found. Confirm that the path in the declaration is correct, and that the file exists on disk.

    Could you help me with that?

    Thank you

    ReplyDelete
  2. Sorry for the delay. Could be the silverlight
    4 tools for Visual Studio.

    Discussion and link to get the tools from microsoft

    http://stackoverflow.com/questions/3591369/silverlight-4-resolving-microsoft-silverlight-csharp-targets-was-not-found

    ReplyDelete