Getting user data (Windows Runtime apps using C#/VB and XAML)

This topic describes how to get a OneDrive user's data, from your Windows Runtime app using C#/VB and XAML.

In this article
Prerequisites
Step 1: Requesting info
Step 2: Requesting info about the user's friends
Step 3: Working with info

To access a user's info from Microsoft OneDrive, your app must check for two things. First, it must confirm that the user has signed in successfully. Second, it must request and receive the user's consent, to work with his or her info. For details about these steps, see Signing users in and Obtaining user consent.

Prerequisites

This topic assumes that your app has initialized the Live SDK successfully, the user has signed in successfully, and the user has consented to the wl.basic scope, which grants access to basic info about the user and his or her contacts.

Step 1: Requesting info

There are two different ways to work with a user's info.

  • The user ID of a person or contact.

  • The me shortcut that provides access to the signed-in user.

This code example shows how to request info about a specific person identified by the user ID, which is represented in this code example as USER_ID. In your code, replace USER_ID with the actual ID string of the user or contact for which you want to get info.

private async void readContact(object sender, RoutedEventArgs e)
{
    try
    {
        LiveOperationResult operationResult = await client.GetAsync("USER_ID");
        dynamic result = operationResult.Result;
        string name = result.first_name;
        // Display user's first name.
    }
    catch (LiveConnectException exception)
    {
        // Display error if operation is unsuccessful.
    }
}

This code example shows how to use the me shortcut to request info about the signed-in user.

...
LiveOperationResult operationResult = await client.GetAsync("me");
...
 

Step 2: Requesting info about the user's friends

This code example shows how to use the me shortcut to get a list of the signed-in user's friends.

...
LiveOperationResult operationResult = await client.GetAsync("me/friends");
...
 

Assuming that your app has permission to access data about the user's friends, you can get that info by using the user ID of a particular friend. In this example, the string value shown as the parameter to GetAsync was in the collection returned by the preceding example. In your code, you would use a value returned by the preceding call and not a string constant as shown in this example.

...
LiveOperationResult operationResult = await client.GetAsync(
    "contact.c1678ab4000000000000000000000000");
...

Step 3: Working with info

To work with info, use methods of the LiveConnectClient class, such as GetAsync, PostAsync, PutAsync, and DeleteAsync for GET, POST, PUT, and DELETE calls, respectively. For example, here's how to get the first name of a user's friend.

private async void btnFriendName_Click(object sender, RoutedEventArgs e)
{
    try
    {
        LiveAuthClient auth = new LiveAuthClient();
        LiveLoginResult loginResult = await auth.LoginAsync(new string[] { "wl.basic" });
        if (loginResult.Status == LiveConnectSessionStatus.Connected)
        {
            this.infoTextBlock.Text = "Signed in.";
        }
        LiveConnectClient connect = new LiveConnectClient(auth.Session);
        LiveOperationResult operationResult = await connect.GetAsync("me/friends");
        dynamic resultList = operationResult.Result;
        StringBuilder friendsNames = new StringBuilder();
        foreach (dynamic result in resultList.data)
        {
            friendsNames.Append(result.name);
            friendsNames.Append("\n");
        }
        this.infoTextBlock.Text = friendsNames.ToString();
    }
    catch (LiveAuthException exception)
    {
        this.infoTextBlock.Text = "Error signing in: " + exception.Message;
    }
    catch (LiveConnectException exception)
    {
        this.infoTextBlock.Text = "Error getting friend(s) name(s): " + exception.Message;
    }
}