Check for Updates within Your Universal Windows Phone App

After reading Saad Mahmood's recent post, Check for Updates of Your App Inside Your Windows Phone Application, I've decided to provide a solution that will work for Windows Phone 8.1 applications developed using the universal app model.

The Windows Phone store allows for automatic installation of updates as they become available but not all users want to allow their mobile device to update apps, especially if they are on their network data connection. Here is a simple solution your apps that will allow you to prompt your users to download the latest update if there is one available.

Getting your app information from the Store

The Windows Phone store gets information about its apps by querying an API endpoint which then returns a response with all the store information about a given app. Using Fiddler4, it is possible to watch the traffic made on your Windows Phone's Wi-Fi connection. If you set this up, navigate to the store and pick an app to view information of, you'll notice that Fiddler records a call to the following API endpoint:

http://marketplaceedgeservice.windowsphone.com/v9/catalog/apps/1a625b44-b542-401b-bdff-52b4357c0573?os=8.10.14219.0&cc=US&lang=en-US&hw=520190979&dm=RM-821_eu_euro1_342&oemId=NOKIA&moId=tmo-gb&cf=99-1

If you take a look at the parts to that URL, you'll quickly notice a query built up with the App ID (i.e. 1a625b44-b542-401b-bdff-52b4357c0573), the version of Windows Phone OS (i.e. os=8.10.14219.0), the country store requesting from (i.e. cc=US), the language of the device (i.e. lang=en-US) and a few other fields including the OEM ID and network provider.

If you navigate to this URL using your web browser, you'll receive an XML file back containing all the information about the application which includes the current version number of the application known to the store. If you're looking to write your own Store application, here's a great place to start, however we're going to look at the important part of the XML file which is the version number.
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <a:feed    
  3.     xmlns:a="http://www.w3.org/2005/Atom"    
  4.     xmlns:os="http://a9.com/-/spec/opensearch/1.1/"    
  5.     xmlns="http://schemas.zune.net/catalog/apps/2008/02">    
  6.     
  7.     <a:entry>    
  8.         <version>0.10.4.40</version>    
  9.     </a:entry>    
  10. </a:feed>  

Getting the Store Version number in C#

Extracting all of the irrelevant information, you're left with a very simple XML file with a version number that can be easily extracted in your C# code. Here's how you can perform the query call in your application and check whether your local version number is different to the one from the store.
  1. public async Task<bool> IsLatestVersion()    
  2. {    
  3.     var packageVersion = Package.Current.Id.Version;    
  4.     
  5.     var formattedVersion = string.Format(    
  6.         "{0}.{1}.{2}.{3}",    
  7.         packageVersion.Major,    
  8.         packageVersion.Minor,    
  9.         packageVersion.Build,    
  10.         packageVersion.Revision);    
  11.     
  12.     var appId = CurrentApp.AppId.ToString();    
  13.     
  14.     var language = CultureInfo.CurrentUICulture.Name;    
  15.     var store = language.Substring(language.Length - 2).ToUpperInvariant();    
  16.     
  17.     using (var client = new HttpClient(new HttpClientHandler(), true))    
  18.     {    
  19.         var queryUrl =    
  20.             string.Format(    
  21.                 "http://marketplaceedgeservice.windowsphone.com/v9/catalog/apps/{0}?os=8.10.14219.0&cc={1}&lang={2}",    
  22.                 appId,    
  23.                 store,    
  24.                 language);    
  25.     
  26.         var data = await client.GetStringAsync(queryUrl);    
  27.     
  28.         var versionElement = ExtractVersionNumber(data);    
  29.     
  30.         if (versionElement != null)    
  31.         {    
  32.             var storeVersion = versionElement.Value;    
  33.     
  34.             var currentVersion = new Version(formattedVersion);    
  35.             var foundVersion = new Version(storeVersion);    
  36.     
  37.             if (foundVersion > currentVersion)    
  38.             {    
  39.                 return false;    
  40.             }    
  41.         }    
  42.     }    
  43.     
  44.     return true;    
  45. }    
  46.     
  47. private static XElement ExtractVersionNumber(string data)    
  48. {    
  49.     var doc = XDocument.Parse(data);    
  50.     var elements = doc.Descendants();    
  51.     
  52.     var versionElement = elements.FirstOrDefault(x => x.Name.LocalName == "version");    
  53.     return versionElement;    
  54. }   
You can call the awaitable IsLatestVersion() method where you'd like to within your application so that you can prompt the user to navigate to the Marketplace when the returning result is false.

Up Next
    Ebook Download
    View all
    Learn
    View all