Enumerating User Profile Data

Ff748764.note(en-us,MSDN.10).gifNote:
Current information about Live Connect is now available in the Windows Live Developer Center. The information in the following sections is provided for legacy purposes only.

To enumerate user profile data, first load the profile data into an in-memory collection object of class DataCollection, and then iterate through the collection. The JavaScript code sample in this topic demonstrates this operation using the WL_Profiles.View scope. WL_Profiles.View allows access to a subset of a user's profile data. For more information about scopes, see Messenger Connect Scopes.

The following example loads data from the Microsoft.Live.Services object of the authenticated user, and gets the firstName property and location property of that user.

dataContext = Microsoft.Live.App.get_dataContext();
dataContext.loadAll(Microsoft.Live.DataType.profiles, profilesLoaded);
function profilesLoaded(dataLoadCompletedEventArgs) {
    var profileCollection = dataLoadCompletedEventArgs.get_data();
    var profile = profileCollection.get_item(0);
    if (profile) {
        var firstName = profile.get_firstName();
        var location = profile.get_location();
    }
}

The preceding example assumes that you already have a data context and an in-memory profile collection. For more information about the Profile resource, see Working with Profiles (JavaScript Library). For an API usage overview, see Using the Data Context.

Profile properties that are not defined will return the value undefined. To avoid this situation, check property values before displaying them, as shown in the following full code sample.

The following sample loads all of the authenticated user's profiles and then displays selected properties.

To construct the sample:

  1. Set up the test project as shown in Getting Started Sample for ASP.NET.).
  2. Create a new ASP.NET page in the project.
  3. Replace the HTML element with the sample code that follows this procedure.
  4. Ensure that the web.config file has the required keys in the WL:APP element.
<%@ Import Namespace="System.Web.Configuration" %>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wl="http://apis.live.net/js/2010">
<head >
    <title>User Profile Data</title>
    <script type="text/javascript" src="http://js.live.net/4.1/loader.js"></script>    
    <script type="text/javascript">
        var dataContext;

        function appLoaded(applicationLoadCompletedEventArgs) {
        }

        function signOut() {
            // Clear profile information.
            $get('ProfileInfo').innerHTML = "";
        }

        function signIn(signInCompletedEventArgs) {
            if (signInCompletedEventArgs.get_resultCode() != Microsoft.Live.AsyncResultCode.success) {
                alert("sign-in failed: " + Microsoft.Live.AsyncResultCode.toString(signInCompletedEventArgs.get_resultCode()));
                return;
            }
            // Get data context and load contacts.
            dataContext = Microsoft.Live.App.get_dataContext();
            dataContext.loadAll(Microsoft.Live.DataType.profile, profilesLoaded);
        }

        function profilesLoaded(dataLoadCompletedEventArgs) {
            if (dataLoadCompletedEventArgs.get_resultCode() !== Microsoft.Live.AsyncResultCode.success) {
                alert("Profiles failed to load: " + Microsoft.Live.AsyncResultCode.toString(dataLoadCompletedEventArgs.get_resultCode()));
                return;
            }
            var profileCollection = dataLoadCompletedEventArgs.get_data();
            var profile = profileCollection.get_item(0);
            if (profile) {
                var br = "<br/>";
                var sb = "<em>Properties viewable with WL_Profiles.View scope.</em>" + br;
                sb += "First Name: " + isDefined(profile.get_firstName()) + br;
                sb += "Last Name: " + isDefined(profile.get_lastName()) + br;
                sb += "Gender: " + 
                      Microsoft.Live.Services.GenderType.toString(profile.get_gender()) + br;
                sb += "Location: " + isDefined(profile.get_location()) + br;
                sb += "Address-State: " + isDefined((profile.get_addresses())[0].get_state()) + br;

                $get('ProfileInfo').innerHTML = sb;              
            }
        }
        function isDefined(value) {
                return (value !== undefined) ? value : "";
        }
    </script>
    <style type="text/css">
    ul { list-style-type: none;}
    </style>
</head>
<body>
    <div>
        <wl:app channel-url="<%=WebConfigurationManager.AppSettings["wl_wrap_channel_url"]%>"
            callback-url="<%=WebConfigurationManager.AppSettings["wl_wrap_client_callback"]%>?<%=SessionId%>"
            client-id="<%=WebConfigurationManager.AppSettings["wl_wrap_client_id"]%>" 
            scope="WL_Profiles.View"
            onload="appLoaded">
        </wl:app>
        Connect to get profile information.<br />
        <wl:signin onsignin="{{signIn}}" onsignout="{{signOut}}"></wl:signin>
        <br />
        <span id="ProfileInfo"></span>
    </div>
</body>
</html>

Show: