Mobile Corner
Search Extensibility in Windows Phone 7
How to integrate your Windows Phone 7 application into the search experience.
- By Nick Randolph
- 11/30/2011
(Editor's Note: Code in bold refers specifically to code in Visual Studio 2010; code in brackets, i.e. [...], refers to code specific to Blend)
Although some say that Windows Phone 7 is playing catch up to iPhone and Android, there are some areas where Microsoft has been able to provide closer integration between third-party applications and the core Windows Phone 7 platform. One such area is Search. As we've seen with the rise of Google, search is an important aspect of our daily lives; by building a better search experience, Microsoft is hoping that Windows Phone 7 will improve productivity in whatever we do.
Before discussing search experience integration, let's take a quick look at the evolution of Windows Phone 7 search. In the original release of Windows Phone 7, the idea of the dedicated search button was two-fold: in some places it would do an in-context search, for example over your contacts in the People hub; elsewhere it would take the user to Bing search. Unfortunately, since there was no ability for third-party applications to respond to the search button, most users found this functionality somewhat confusing. This was addressed in the Mango update: the search button always directs the user to Bing search.
Recognizing that application developers still want to integrate into the search experience, Microsoft added search extensibility to the platform. Rather than leaving it to each application to implement search, or serve up search results, search extensibility on Windows Phone 7 allows applications to be listed as providers of additional information for a number of search result categories. When a user searches for a product, a place or a movie, depending on his or her region, they may see a quick card (the left image of Figure 1) as part of the search results. Clicking the quick card lists more information about the search result, including a list of applications (the right image of Figure 1). Clicking on an application will launch that application, supplying information about the search result to the application so it can provide additional information, or perhaps sales information such as price or availability.
[Click on image for larger view.] |
Figure 1. A quick card in Windows Phone 7, showing general and specific search results. |
There are two main parts to integrating your application with search: Registration and Implementation. Registration involves modifying your WMAppManifest.xml file to indicate which quick cards and corresponding categories your application should be registered for, and supplying the Extras.xml file which provides information about how your application should be listed in the search results. Implementation involves handling the deep link URI launched within your application, when the user clicks on your application from the search results.
In the WMAppManifest.xml file, you need to list the quick card categories that your application should be listed against. For example, the following XML, which should be placed before the closing App element (i.e. before </App>), registers the application for the "Food and Dining", "Travel", and "Arts and Entertainment" place categories. The full list of categories is available via MSDN Reference.
<Extensions>
<Extension ExtensionName="Bing_Places_Food_and_Dining"
ConsumerID="{5B04B775-356B-4AA0-AAF8-6491FFEA5661}"
TaskID="_default" ExtraFile="Extensions\\Extras.xml" />
<Extension ExtensionName="Bing_Places_Travel"
ConsumerID="{5B04B775-356B-4AA0-AAF8-6491FFEA5661}"
TaskID="_default" ExtraFile="Extensions\\Extras.xml" />
<Extension ExtensionName="Bing_Places_Arts_and_Entertainment"
ConsumerID="{5B04B775-356B-4AA0-AAF8-6491FFEA5661}"
TaskID="_default" ExtraFile="Extensions\\Extras.xml" />
</Extensions>
You'll notice that each of the Extension nodes references Extras.xml, which needs to be in the Extensions folder within your application. Simply create a folder with the name Extensions, and create a new XML file called Extras.xml. Into this file add the necessary elements to describe the application. The following XML specifies the application title (for all search results) and the caption (for the specified categories):
<?xml version="1.0" encoding="utf-8" ?>
<ExtrasInfo>
<!-- Application title-->
<AppTitle>
<default>Search Extensibility App</default>
</AppTitle>
<!-- Search-related captions -->
<Consumer ConsumerID="{5B04B775-356B-4AA0-AAF8-6491FFEA5661}">
<!-- Computer products caption -->
<ExtensionInfo>
<Extensions>
<ExtensionName>Bing_Places_Food_and_Dining</ExtensionName>
<ExtensionName>Bing_Places_Travel</ExtensionName>
<ExtensionName>Bing_Places_Arts_and_Entertainment</ExtensionName>
</Extensions>
<CaptionString>
<default>Additional place information</default>
</CaptionString>
</ExtensionInfo>
</Consumer>
</ExtrasInfo>
If you want to supply alternate titles and captions for different languages and regions, add the appropriate language string immediately after the default elements. For example, in order to add French, you would specify "<fr-FR>{French title}</fr-FR>". You can also provide different captions for search categories by adding additional ExtensionInfo elements.
When the user clicks on your application in the search results, Windows Phone 7 will attempt to launch your application and navigate to a URI that starts with /SearchExtras. For example, the following is for a place search result. Different quick cards have a different set of query string parameters.
/SearchExtras?PlaceName=Fourth Coffee&PlaceLatitude=47.64602&PlaceLongitude=-122.135074&PlaceAddress=
1234 Microsoft Way, WA, 98101&Category=Bing_Places_Food_and_Dining
Rather than creating a page called SearchExtras, which would be impossible, instead use a UriMapper to redirect the navigation to a page within your application. For example, the following UriMapper, defined in the Resources element of App.xaml, can be wired to the PhoneApplicationFrame in App.xaml.cs to redirect the search URI to MainPage.xaml.
App.xaml
<Application.Resources>
<uriMapper:UriMapper x:Key="UriMapper">
<uriMapper:UriMapping Uri="/SearchExtras"
MappedUri="/MainPage.xaml" />
</uriMapper:UriMapper>
</Application.Resources>
App.xaml.cs
RootFrame = new PhoneApplicationFrame() {
UriMapper = Resources["UriMapper"] as UriMapper
};
Now all that's needed is to extract the query string parameters within the page. You might do this in the OnNavigatedTo method on the page, as in this example:
protected override void OnNavigatedTo(NavigationEventArgs e) {
base.OnNavigatedTo(e);
var placeName = NavigationContext.QueryString["PlaceName"];
MessageBox.Show(placeName);
}
In this article you've seen how easily you can connect your application into the search experience on a Windows Phone 7 device. Your application may be listed against search results, even if your application is not installed on the phone. This makes it a great way to feature your application and encourage users to download and/or purchase your application.
About the Author
Nick Randolph runs Built to Roam, a consulting company that specializes in training, mentoring and assisting other companies build mobile applications. With a heritage in rich client applications for both the desktop and a variety of mobile platforms, Nick currently presents, writes and educates on the Windows Phone platform.