SpriteHand
Module Border
  Using the Live Search API 2.0 with Silverlight
Module Border
Location: BlogsAndy's Blog    
Posted by: host 11/17/2008 7:18 PM

The Live Search API allows you to submit queries for Web Pages, Images, and other search types to the Live.com search service and then process results in whatever evil way you like :) When I'm training or presenting Silverlight, I like to use Live.com as a way to show Silverlight's web service integration and the power of RIA's in mashup scenarios.

The Live Search API was recently updated to version 2.0, and there are some changes to how you interact with the API. In this blog post, I'll give a quick and dirty step-by-step on how to interact with the new API.

1. Go to http://search.live.com/developers and create an API ID

2. Create a new SL solution and in the WEB project, right-click and select "Add Service Reference" and then enter this URL, where MY_APP_ID is the API ID you were given in step 1:

 http://api.search.live.net/search.wsdl?MY_APP_ID

 * When adding the service reference, be sure to enter LiveSearchService in the Namespace field.

3. In the Web Project, Create a new Silverlight-Enalbed WCF Web Service and name it LiveSearchWrapper.svc

4. In the Web Service, Add a new WebMethod to search for Web Pages, and a new Class to store and return results:

       [OperationContract]
        public List<SearchResult> DoSearch(string search)
        {
            LiveSearchService.LiveSearchPortTypeClient s = new LiveSearchPortTypeClient();
           
            SearchRequest request = new SearchRequest();

            // BE SURE TO ENTER _YOUR_ APP ID HERE!!!
            request.AppId = "MY_APP_ID";
            request.Query = search;
            request.Sources = new SourceType[] { SourceType.Web };

            request.Version = "2.0";
            request.Market = "en-us";
            request.Adult = AdultOption.Moderate;
            request.AdultSpecified = true;

            SearchResponse response = s.Search(request);


            // create a collection of results
            List<SearchResult> lstWebPages = new List<SearchResult>();

            foreach (WebResult foundItem in response.Web.Results)
            {
                // web page hit
                SearchResult result = new SearchResult();
                result.ResultUrl = foundItem.Url;
                result.Description = foundItem.Description;
                lstWebPages.Add(result);
            }

            // send 'em down to the client
            return lstWebPages;

        }

        public class SearchResult
        {
            public int HitNumber { get; set; }
            public string ResultUrl { get; set; }
            public string ImageUrl { get; set; }
            public string Description { get; set; }
            public double ImageHeight { get; set; }
            public double ImageWidth { get; set; }
        }

 

3. In the Silverlight Project, open Page.xaml and add some controls and layout INSIDE the <Grid> to search and display. Note that we are adding a search box, a search button, and a nicely formatted data template to the ListBox rows:

       <Grid.RowDefinitions>
            <RowDefinition Height="25" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <StackPanel Orientation="Horizontal">
            <TextBox x:Name="txtSearch" Width="200" />
            <Button x:Name="btnSearch" Content="Search" />
           
        </StackPanel>
           
           
        <ListBox x:Name="lstResults" Grid.Row="1" Height="257" Width="388" RenderTransformOrigin="0.5,0.5" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical" Margin="5,5,5,5" Width="250">
                        <HyperlinkButton NavigateUri="{Binding Path=ResultUrl}" TargetName="_blank" Content="{Binding Path=ResultUrl}" />
                        <TextBlock Text="{Binding Path=Description}" TextWrapping="Wrap" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

4. Add a Click Event handler to btnSearch like so:

       private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            DoSearch();
        }

5. Add code to call the web service DoSearch method asynchronously (note that LiveSearchNewApi should be replaced with your Project name):

  private void DoSearch()
        {
            LiveSearchWrapper.LiveSearchWrapperClient webSvc = new LiveSearchNewApi.LiveSearchWrapper.LiveSearchWrapperClient();
            webSvc.DoSearchCompleted += new EventHandler<LiveSearchNewApi.LiveSearchWrapper.DoSearchCompletedEventArgs>(webSvc_DoSearchCompleted);
            webSvc.DoSearchAsync(txtSearch.Text);
        }

        void webSvc_DoSearchCompleted(object sender, LiveSearchNewApi.LiveSearchWrapper.DoSearchCompletedEventArgs e)
        {
           
            lstResults.ItemsSource = e.Result;
        }

That's it! Run the solution and try entering some search terms and then click the search button. Note that in the web service search, we are specifying a SourceType of Web, but the Live Search service offers many other Source Types including Image and Video.

Permalink |  Trackback

Comments (1)   Add Comment
Re: Using the Live Search API 2.0 with Silverlight    By Anonymous on 4/16/2009 5:34 AM
i got an error in LivesearchWrapper


Title:
Comment:
Add Comment   Cancel 
Module Border Module Border
Module Border
  Subscribe
Module Border
RSS   Twitter
Module Border Module Border
Module Border
  Diversions
Module Border

TALKING RAGDOLL
This Windows Phone 7 App was created using Silverlight, the  Physics Helper Library,  and the Farseer Physics Engine. It gets interesting when you import your friends photos and have your way with them!

MORE INFO



DROPPYPOP
This Windows Phone 7 game was created using Silverlight, the  Physics Helper Library,  and the Farseer Physics Engine.
DEMO

MORE INFO



BOSS LAUNCH
This physics game won first place in the Server Quest Contest. Created using Silverlight 2, the Physics Helper Library,  and the Farseer Physics Engine.
PLAY IT

MORE INFO



DESTROY ALL INVADERS
A scrolling shooter game where the objective is to destroy the invading UFO's flying over a neighborhood of your choosing. Imagery provided by Microsoft Virtual Earth. Created using Silverlight 2.
PLAY IT

INFO AND CODE



PHYSICS HELPER DEMOS
These demos were created for the Physics Helper Library, which makes it easy to create physics games and simulations using Expression Blend, Silverlight, and the Farseer Physics Engine.
PLAY IT

INFO AND CODE



HOOK SHOT
This little basketball game took first place in the TeamZoneSports Silverlight Contest. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



SORT THE FOOBARS
A game where you need to sort the good foobars from the bad ones. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



POLYGON PHYSICS DEMO
A demo showing polygon physics where the user draws physics objects with the mouse. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



SILVERLIGHT ROCKS!
Destroy the asteroids before they destroy your ship! Created using Silverlight 2.
PLAY IT

INFO AND CODE



FISH GAME
A simple game of harpoon-the-fish. Written using the AJAX Sprite Toolkit.
PLAY IT

INFO AND CODE

Module Border Module Border
Module Border
  Search_Blog
Module Border
Module Border Module Border
Module Border
  Blog_Archive
Module Border
Module Border Module Border
Copyright (c) 2013 andy.beaulieu.com - Login