How to get your Windows Phone 7 application id directly in your application
Saturday, March 24, 2012 4:44 PM
Vivien Chevallier
So that you don't need to submit your application to get it's id and resubmit it.

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:

  • Submit your application to the marketplace so that you can get the real application id.
  • Then update your application with the new application id wherever you need it.
  • To finish resubmit your application.

 

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:

Download full sources

 

(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.

Comments

Sunday, July 01, 2012 7:14 PM - Taynah
English
Most help articles on the web are inaccurate or incoherent. Not this!

Add a comment

(Will not be published)

Back to articles