For my future WP7 application Warnygo, I needed to get its id for several reasons. For example, use it with the ShareLinkTask or send it to my services.
As you may know, you can find your application id in the WMAppManifest.xml. It's the ProductID attribute in the App node.
When you first create your application with Visual Studio, you get a random one. But you will get a new one through the marketplace submission process. So how to get your application id in your application once submitted?
Well, the ugly way is to:
Personally I don't like this way!
The clean way that I propose is to directly read the application id in the WMAppManifest.xml file. Check this code:
using System; using System.Xml.Linq; namespace PhoneApp.ApplicationId { public static class ApplicationProperties { public static Guid GetId() { Guid applicationId = Guid.Empty; var productId = XDocument.Load("WMAppManifest.xml").Root.Element("App").Attribute("ProductID"); if (productId != null && !string.IsNullOrEmpty(productId.Value)) Guid.TryParse(productId.Value, out applicationId); return applicationId; } } }
Example of use
using System; using System.Windows; using Microsoft.Phone.Controls; using Microsoft.Phone.Tasks; namespace PhoneApp.ApplicationId { public partial class MainPage : PhoneApplicationPage { public MainPage() { InitializeComponent(); } private void ButtonShowId_Click(object sender, RoutedEventArgs e) { MessageBox.Show(ApplicationProperties.GetId().ToString(), "My id is:", MessageBoxButton.OK); } private void ButtonShare_Click(object sender, RoutedEventArgs e) { ShareLinkTask shareLinkTask = new ShareLinkTask(); shareLinkTask.Title = "Check this app!"; shareLinkTask.LinkUri = new Uri(string.Format("http://www.windowsphone.com/s?appid={0}", ApplicationProperties.GetId()), UriKind.Absolute); shareLinkTask.Show(); } } }
Summary
We have seen how to get a Windows Phone 7 Application id. If like me you have your own WP7 framework, you should add this extension!
You can download the example solution here:
(Note that the project uses the Windows Phone SDK 7.1)
As usual, if you have any questions about this article please feel free to contact me or leave a comment in the section bellow.
Most help articles on the web are inaccurate or incoherent. Not this!