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.