Quickstart: Personalizing your app (XAML)

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

Your Windows Runtime app using C++, C#, or Visual Basic can use Windows Live Services to access info from the user's Microsoft account profile by using the Live SDK.

When your app runs, the user must sign in with his or her Microsoft account and give consent for your app to access his or her data. After the user signs in and gives consent, your app can access the user's profile data to personalize the experience. This topic shows you how to do each of these steps.

Important  This quickstart is for illustration and demonstration purposes only. To use this feature in an app that you want to upload to the Windows Store and distribute to customers, you'll need to include a sign-out feature and a privacy policy as well as a sign-in feature. After you complete this quickstart, read Requirements for Microsoft account sign-in to see how to add these features.

 

Important  The tutorial in this topic demonstrates a Windows Store app. You can also add Microsoft services to a Windows Phone Store app.

 

Prerequisites

  • Microsoft Visual Studio installed
  • The Live SDK installed
  • A Windows Store app using C# project open in Visual Studio

Add the reference to the Live SDK

When your app runs, it can display the Windows sign-in control for the user sign in using their Microsoft account. After signing in, the user is prompted to give consent for the app to use his or her data, the he or she hasn't already given consent.

To display the Windows sign-in control when your app runs, do this in your project:

  1. In Solution Explorer, right-click References > Add Reference....
  2. Click Windows > Extensions to display the list of references that can be added to your program.
  3. Check the box next to Live SDK. If you don't see Live SDK in the list, reinstall the Live SDK and retry this procedure.
  4. Click OK to exit the dialog box.

Add the using statements to your app

At the beginning of each C# code file in your app that refers to the Live SDK, for example, MainPage.xaml.cs and the code-behind files of your app's UI, add the following using statements.

using Microsoft.Live;

Add the UI element for the personalized greeting

Add an element in your app's layout to display the user's name.

The actual Extensible Application Markup Language (XAML) code that you use to do this depends on your app's layout. This example shows one way that you can add this to your app's layout; however, it doesn't include any styling or placement properties that you'll probably want to include when you add this to your app.

<TextBlock x:Name="userName" Text="You're not signed in." />

Add the code that gets the user's Microsoft account info

In the code behind file of the page in which you added the markup in the previous step, add code to get the user's profile info from their Microsoft account and update the text in the page.

This code example does the following:

  1. Initializes the Live SDK
  2. Signs in to the user's Microsoft account. If the user isn't connected, they'll see the Microsoft account sign-in screen to sign in.
  3. Gets consent to access the user's profile info. If the user hasn't given consent for this app to access their info, they'll see the UI that asks for their consent.
  4. Gets the user's info from their Microsoft account profile.
  5. Updates the app's UI with the personalized greeting.
try
{
    // Initialize access to the Live Connect SDK.
    LiveAuthClient LCAuth = new LiveAuthClient();
    LiveLoginResult LCLoginResult = await LCAuth.InitializeAsync();
    try
    {
        // Sign in to the user's Microsoft account with the required scope.
        //    
        //  This call will display the Microsoft account sign-in screen if the user 
        //  is not already signed in to their Microsoft account through Windows 8.
        // 
        //  This call will also display the consent dialog, if the user has 
        //  has not already given consent to this app to access the data described 
        //  by the scope.
        // 
        LiveLoginResult loginResult = await LCAuth.LoginAsync(new string[] { "wl.basic" });
        if (loginResult.Status == LiveConnectSessionStatus.Connected)
        {
            // Create a client session to get the profile data.
            LiveConnectClient connect = new LiveConnectClient(LCAuth.Session);

            // Get the profile info of the user.
            LiveOperationResult operationResult = await connect.GetAsync("me");

            // Format the text to display and update the element in the layout.
            dynamic result = operationResult.Result;
            if (result != null)
            {
                this.userName.Text = string.Join(" ", "Hello", result.name, "!");
            }
            else
            {
                this.userName.Text = "Unable to get your name.";
            }
        }

    }
    catch (LiveAuthException exception)
    {
        // handle the login, scope, or request exception
    }
}
catch (LiveAuthException exception)
{
    // handle the initialization exception
}
catch (LiveConnectException exception)
{
    // handle the Live Connect API exception
}

Test your personalized app

When you run your personalized app, you might notice that you don't always see the Microsoft account sign-in screen. This could be because Windows remembers your sign-in info to make it easy for your users to have a personalized experience the next time they run your app.

Important  To access user's data from an app that you want to upload to the Windows Store and distribute to customers, you'll need to include a sign-out feature as well as a sign-in feature and a privacy statement. The sign-in and sign-out implementation is described in the Requirements for Microsoft account sign-in.

 

Summary and next steps

In this topic, you learned how to begin using Windows Live Services to access users' data in Microsoft cloud services like Outlook.com and Microsoft OneDrive from your Windows Runtime app using C++, C#, or Visual Basic.

You learned how to let users sign in by using a Microsoft account and, get their consent so that your app can access their data.

Continue learning more about using Windows Live Services in the Requirements for Microsoft account sign-in, where you can find out how to add sign-in and sign-out functions.

For more info about what your app can do with the user's profile data, see Getting user data in the OneDrive documentation.