Getting user data (Windows Runtime apps using JavaScript and HTML)

This topic shows you how to get a user's info on Microsoft OneDrive, from your Windows Runtime apps using JavaScript and HTML.

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

function readContact() {
    WL.api({
        path: "USER_ID",
        method: "GET"
    }, onReadContactCallback);
}

function onReadContactCallback(response) {
    if (!response.error) {
        var name = response.first_name;
        // Display user's first name.
    }
}

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

...
WL.api({
    path: "me",
    method: "GET"
}, onApiCalled);
... 

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.

...
WL.api({
    path: "me/friends",
    method: "GET"
}, onApiCalled);
... 

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 for the path parameter 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.

...
WL.api({
    path: "contact.c1678ab4000000000000000000000000",
    method: "GET"
}, onApiCalled);
... 

Step 3: Working with info

To work with info, use the WL.api method. For example, here's how to make a GET call to get the first name of a user's friend.

function getFriendName_onClick() {
    WL.login({
        scope: "wl.basic"
    }).then(
        function (response)
        {
            WL.api({
                path: "contact.09fb5b27000000000000000000000000",
                method: "GET"
            }).then(
                function (response) {
                    document.getElementById("infoArea").innerText =
                        "Friend's name is: " + response.first_name;
                }, 
                function (responseFailed) {
                    document.getElementById("infoArea").innerText =
                        "Error calling API: " + responseFailed.error.message;
                }
            );
        },
        function (responseFailed) {
            document.getElementById("infoArea").innerText =
                "Error signing in: " + responseFailed.error_description;
        }
    );
}