Listing User Activities

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

This topic demonstrates how to enumerate an authenticated user's activities. The user's activities are Microsoft.Live.Services.Activity items that are specific to that authenticated user. These are different from the activities of the user's contacts. For information about listing contact activities, see Listing Contact Activities.

Activities, like contacts and profiles, are requested through the data context that is available after a user has signed in and consented. The specific consent scope for a user's activities is the myActivities field of the DataType object.

The following code example demonstrates how to load the currently authenticated user's activities. (A full sample is shown later, in the Samples section.)

function getActivities() {
    Microsoft.Live.App.get_dataContext().loadAll(Microsoft.Live.DataType.myActivities, myActivitiesLoaded);
}
function myActivitiesLoaded(dataLoadCompletedEventArgs) {
    var myActivitiesCollection = dataLoadCompletedEventArgs.get_data();
    for (var i = 0; i < myActivitiesCollection.get_length(); i++)
        var activity = myActivitiesCollection.getItem(i);
        var title = activity.get_title();
    }
}

In the preceding example, the collection of activities is in the variable myActivitiesCollection. Use the getItem method of the DataCollection class to get a single Microsoft.Live.Services.Activity item.

The number of activities returned can be large. To aid the user by showing smaller amounts of information, you can specify query parameters when you load the activities from the data context; this is called server-side filtering. You can also use a View object to page and filter the displayed results; this is called client-side filtering. The following code example demonstrates how to load the currently authenticated user's activities and request only the most recent ten activities. Note that the load method is used instead of the LoadAll method. (A full sample is shown later, in the Samples section.)

function getActivities() {
    var options = { "$top": "10"};
    Microsoft.Live.App.get_dataContext().load(Microsoft.Live.DataType.myActivities, options, myActivitiesLoaded);
}
function myActivitiesLoaded(dataLoadCompletedEventArgs) {
    var myActivitiesCollection = dataLoadCompletedEventArgs.get_data();
    for (var i = 0; i < myActivitiesCollection.get_length(); i++)
        var activity = myActivitiesCollection.getItem(i);
        var title = activity.get_title();
    }
}

The preceding code example assumes that you already have a data context and an in-memory activities collection.

For more information about the Activities resource, see Working with Activities (JavaScript Library). For an API usage overview, see Using the Data Context. For more information about data filtering and views, see Data Filtering and Views.

The following sample shows a complete ASP.NET page that lists a user's activities. The example uses the load method of the Microsoft.Live.Services.DataContext object, which takes an options parameter that specifies how to filter the requested resources. In this sample, only a specific number of the most recent activities are retrieved.

To test this page, set up the Getting Started Sample for ASP.NET, and then add this page to that project as follows:

  1. Create a new ASP.NET page in the project.
  2. Replace the HTML element with the sample code.
  3. Ensure that the web.config file has the required keys in the <wl:app> tag.
<%@ Import Namespace="System.Web.Configuration" %>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wl="http://apis.live.net/js/2010">
<head>
    <title>Activities List</title>
    <script type="text/javascript" src="http://js.live.net/4.1/loader.js"></script>
    <script type="text/javascript">
        var dataContext;
        var myActivitiesCollection;

        function appLoaded(applicationLoadCompletedEventArgs) {
            if (Microsoft.Live.App.get_auth().get_state() === Microsoft.Live.AuthState.authenticated) {
                getActivities();
            }
        }
        function signOut() {
            $get('ActivitiesList').innerHTML = "";
        }
        function signIn(signInCompletedEventArgs) {
            if (signInCompletedEventArgs.get_resultCode() != Microsoft.Live.AsyncResultCode.success) {
                alert("sign-in failed: " + signInCompletedEventArgs.get_resultCode());
                return;
            }
            // Get LiveDataContext and load contacts.
            dataContext = signInCompletedEventArgs.get_dataContext();
            getActivities();
        }
        function getActivities() {
            options = { "$top": String.format("{0}", $get('selShow').value) };
            Microsoft.Live.App.get_dataContext().load(Microsoft.Live.DataType.myActivities, options, myActivitiesLoaded);
        }

        function myActivitiesLoaded(dataLoadCompletedEventArgs) {
            if (dataLoadCompletedEventArgs.get_resultCode() !== Microsoft.Live.AsyncResultCode.success) {
                alert("Activities failed to load! Are you signed in?");
                return;
            }
            myActivitiesCollection = dataLoadCompletedEventArgs.get_data();
            createListOfActivities(myActivitiesCollection);

        }
        // List only a "view" of the activities.
        function createListOfActivities(ActivitiesCollection) {
            var list = $get('ActivitiesList');
            list.innerHTML = '';
            for (var i = 0; i < ActivitiesCollection.get_length(); i++) {
                if (ActivitiesCollection.getItem(i) !== undefined) {
                    var activity = ActivitiesCollection.getItem(i);
                    list.innerHTML += "<li>";
                    list.innerHTML += activity.get_title();
                    list.innerHTML += "</li>";
                }
            }
        }
    </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_Activities.View"
            onload="appLoaded">
        </wl:app>
        <wl:signin onsignin="{{signIn}}" onsignout="{{signOut}}"></wl:signin>
        <br /><hr /> 
        <wl:userinfo/>
        <br /><br /><br />
        <h3>Your Recent Activities</h3>
        Show 
        <select id="selShow">
            <option value="1">1</option>
            <option value="5" selected="selected">5</option>
            <option value="10">10</option>
            <option value="20">20</option>
        </select>
        <input type="button" value="Refresh" onclick="getActivities()" />
        <ul id="ActivitiesList">
        </ul>
    </div>
</body>
</html>

Show: