[Windows Phone] Access your Windows Live data easily thanks to the Live SDK !

December 7th, 2011 | Posted by Tom in .NET | Article | Windows Phone | Read: 4,851

It’s a common usage, these days, to develop applications that allow users to access their personal data and Windows Live (documents, pictures, etc.) is a good example of the kind of data someone might want to access from everywhere.

Until now, developers needed to develop custom systems to access the data on the cloud, when it was possible. But thanks to Microsoft and it’s Live SDK, it’s possible to access any Windows Live data from Windows Phone or Windows 8 application (HTML or XAML).

First thing to do is to create Client ID for the application you want to develop. This ID will be used to authenticate your application and ensure that user can trust it. To create your Client ID, go to https://manage.dev.live.com and create an application (simply provide its name and its language):

image

Once the application is created, don’t forget to edit its settings to indicate that it’s a mobile app and not a desktop app:

image

Now, it’s start to develop the Windows Phone application. Add a reference to the 2 DLLs provided in the SDK (C:\Program Files (x86)\Microsoft SDKs\Live\v5.0\Windows Phone\References):

  1. Microsoft.Live.Controls.dll
  2. Microsoft.Live.dll

Now, in your XAML file, add the following code, which will provide you a button to allow users to connect to their Live account:

<Live:SignInButton ClientId="XXXXXXX"
                   Scopes="wl.basic wl.photos wl.skydrive wl.offline_access wl.signin wl.skydrive_update"
                   RedirectUri="http://oauth.live.com/desktop"
                   Branding="Skydrive"
                   TextType="SignIn"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Center"
                   SessionChanged="OnSessionChanged" />

There are a lot of properties that you can changed (you can see the whole documentation here: http://msdn.microsoft.com/en-us/library/hh243650.aspx) but the most important are:

  1. ClientId: This is the number that you’ve generated in the first part
  2. Scopes: The list of “features” that your application will be allowed to manipulate. In our example, the application will get access to basic data, photos, Skydrive, ability to reconnect, etc.

In the code-behind, add the following code, used to be notified when the user is connected and then, get access to its name:

private void OnSessionChanged(object sender, Microsoft.Live.Controls.LiveConnectSessionChangedEventArgs e)
{
    if(e.Error == null)
    {
        if (e.Status == LiveConnectSessionStatus.Connected)
        {
            this._currentSession = e.Session;

            var client = new LiveConnectClient(this._currentSession);
            client.GetCompleted += (o, args) =>
            {
                if (args.Error == null)
                {
                    MessageBox.Show(Convert.ToString(args.Result["name"]));
                }
            };
            client.GetAsync("me");
        }
    }
}

Now, when user execute your application, it get a button he can use to log on Windows Live:

image image

Once logged in, a page display which data the application will be able to use:

image

Then, the user is authenticated and the code to retrieved its name is executed:

image

Of course, this sample is pretty simple and it’s possible to perform more complex task such as upload/downloading pictures, etc.

 

Feel free to take a look by yourself!

 

Happy coding!

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

One Response

Add Comment Register



Leave a Reply