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; }
        }
  
  
    }
  
    }










Wednesday, April 6, 2011

VB,NET to Wordpress Example

Been trying to wrap my head around manipulating a php wordpress install with a .NET client.

Not much clear cut info out there so I figured I would post a quick example to give those interested a starting point.

First things first.  You need the  CookComputing.XmlRpc library. You can download the latest version here.

You need to extract the zip file using winzip, winrar or another non system extractor. 

For some reason the bin directory is not visible if using the system extractor even if you are running as Admin and have all folder view options on.

Next, start up visual studio and create a new standard windows forms application.  You should not target a compact framework. I had problems with the .net 4 client profile framework so I changed the framework to standard .net 4.0

Now you will need to add two references to the project.  Click add reference from visual studio and select browse.  You need to browse to the bin directory of the xml-rpc.net.2.5.0 directory you previously extracted.  Add a reference to   CookComputing.XmlRpcV2.dll  and IStateName.dll

Go back to your form and add a single button. View the form code window and add the following.

Imports CookComputing.XmlRpc
Imports System

Public Class Form1 'your form name
    Public Structure category
        Public categoryId As Object
        Public parentId As Object
        Public description As Object
        Public categoryName As Object
        Public htmlUrl As Object
        Public rssUrl As Object
    End Structure
 '// important to have the correct wordpress xmlrpc.php path.  If in doubt, use your browser and navigate to it '//first.

 <XmlRpcUrl("http://yoursite.com/wordpress/xmlrpc.php")> _
    Public Interface IWP
        Inherits IXmlRpcProxy

        <XmlRpcMethod("wp.getCategories")> _
        Function getCategories(ByVal args() As String) As category()
    End Interface
'// your button1 code....
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim proxy As IWP = XmlRpcProxyGen.Create(Of IWP)()
        Dim args() As String = {"http:/yoursite.com", _
                                  "AdminUserName", "AdminPassword"}
        Dim categories() As category
        categories = proxy.getCategories(args)
        For Each category In categories
            Debug.Print(category.categoryId)
            Debug.Print(category.categoryName)
            Debug.Print(category.description)
            Debug.Print(category.htmlUrl)
            Debug.Print(category.rssUrl)
        Next

    End Sub
End Class


That should do it. 

There is another great blog that defines more functions step by step in C# and VB.net here