Check for Updates of Your App Inside Your Windows Phone Application

Many of times you find crashes in your application and you feel that you should prepare an update for the application. After you have prepared an update for your application you upload it and it is notified of your store. Wouldn't it be great to notify the user that an update for this very application is available.

When your phone searches for update, it goes to the following link.

    http://marketplaceedgeservice.windowsphone.com/v8/catalog/apps/<application id>?os=8.0.9903.0&cc=GB&oc=&lang=en-GB&hw=520170499&dm=RM-821_eu_euro1&oemId=NOKIA&moId=VOD-GB&cf=99-1
The response of the link is really amazing it responds to XML format data with all new version details, and the stuff Store gets.

Now how we can do this all stuff in our application?

Well that is not much difficult all you have to do is just write a piece of code.

For checking that is the application's update is available over store or not you need to get the ProductID of your application here is link below from where you can get your ProductID &Read WMAppManifest.xml.

For the current running application you need to read its WMAppManifest.xml file.

Code is below:
  1. public static string GetManifestAttributeValue(string attributeName)  
  2. {  
  3.     var xmlReaderSettings = new XmlReaderSettings  
  4.     {  
  5.         XmlResolver = new XmlXapResolver()  
  6.     };  
  7.   
  8.     using (var xmlReader = XmlReader.Create("WMAppManifest.xml", xmlReaderSettings))  
  9.     {  
  10.         xmlReader.ReadToDescendant("App");  
  11.   
  12.         return xmlReader.GetAttribute(attributeName);  
  13.     }  
  14. }  
So, now we are going to write the code to lookup if there is any update for app available as there is no option in the current SDK to get any info from STORE:
  1. private Task<Version> GetUpdatedVersion()  
  2. {  
  3.     var cultureInfoName = CultureInfo.CurrentUICulture.Name;  
  4.   
  5.     var url = string.Format("http://marketplaceedgeservice.windowsphone.com/v8/catalog/apps/{0}?os={1}&cc={2}&oc=&lang={3}​",  
  6.         GetManifestAttributeValue("ProductID"),  
  7.         Environment.OSVersion.Version,  
  8.         cultureInfoName.Substring(cultureInfoName.Length - 2).ToUpperInvariant(),  
  9.         cultureInfoName);  
  10.   
  11.     var request = WebRequest.Create(url);  
  12.   
  13.     return Task.Factory.FromAsync(request.BeginGetResponse, result =>  
  14.     {  
  15.         var response = (HttpWebResponse)request.EndGetResponse(result);  
  16.   
  17.         if (response.StatusCode != HttpStatusCode.OK)  
  18.         {  
  19.             throw new WebException("Http Error: " + response.StatusCode);  
  20.         }  
  21.   
  22.         using (var outputStream = response.GetResponseStream())  
  23.         {  
  24.             using (var reader = XmlReader.Create(outputStream))  
  25.             {  
  26.                 reader.MoveToContent();  
  27.   
  28.                 var aNamespace = reader.LookupNamespace("a");  
  29.   
  30.                 reader.ReadToFollowing("entry", aNamespace);  
  31.   
  32.                 reader.ReadToDescendant("version");  
  33.   
  34.                 return new Version(reader.ReadElementContentAsString());  
  35.             }  
  36.         }  
  37.     }, null);  
  38. }  
This is the async method to get the latest version from store, also what we need is to compare both versions of application. How come we do it? We have to make another method to check it.

Here you go,
  1. private async void CheckForUpdatedVersion()  
  2. {  
  3.     var currentVersion = new Version(GetManifestAttributeValue("Version"));  
  4.     var updatedVersion = await GetUpdatedVersion();  
  5.   
  6.     if (updatedVersion > currentVersion  
  7.         && MessageBox.Show("Do you want to install the new version now?""Update Available", MessageBoxButton.OKCancel) == MessageBoxResult.OK)  
  8.     {  
  9.         new MarketplaceDetailTask().Show();  
  10.     }  
  11. }  
So now just call method,

CheckForUpdatedVersion() in your application and use it.

Reference: Checking for updates from inside a Windows Phone app.

Personal Blog: Blend Unleashed.