Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Displaying Details for Activities

Ff750429.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 list details of an authenticated user's activities. You might use such details to build a full-featured activity stream implementation. A user's activities are Microsoft.Live.Services.Activity items that are specific to that authenticated user. These activities are different from the activities of the user's contacts. For information about listing the activities of a user's contacts, see Listing Contact Activities. However, you can use the ideas presented here to create a rich display for contact activities as well.

Activities, such as 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 and get details. (A full sample is shown later, in the Samples section.) The example assumes you already have a data context and an in-memory activities collection.

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 htmlSummary = activity.get_htmlSummary();
        var pubDate = activity.get_published();
        var htmlContent = (activity.get_htmlContent() !== undefined) ? activity.get_htmlContent() : "";
        var lastReply = (activity.get_lastReplyTime() !== undefined) ? activity.get_lastReplyTime() : "";
        var repliesCount = (activity.get_repliesCount() !== undefined) ? activity.get_repliesCount() : 0;
        var repliesLink = activity.get_repliesLink();

    }
}

If no value exists for an activity-related property, the value "undefined" is returned. Therefore, when you work with the properties of an activity, check the value first before displaying it in the user interface.

For more information about the activity resource, see Working with Activities (JavaScript Library). For an API usage overview, see Using the Data Context.

The following sample shows a complete ASP.NET page that lists details of 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:

  1. Follow the instructions in Getting Started Sample for ASP.NET.
  2. Create a new ASP.NET page in the sample 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> 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;
        var displayTemplate = "{0}<br/>" +
                              "<div>{1}</div></br>" +
                              "<strong>Published:</strong> {2} + <br/>" +
                              "<strong>Replies: </strong> {3} (<a href=\"{4}\">reply<\/a>) </br>";
        var lastReplyTemplate = "<strong>Last Reply: </strong> {1}";

        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 += getActivityProperties(activity);
                    list.innerHTML += "<hr/></li>";
                }
            }
        }
        function getActivityProperties(activity) {
            var htmlSummary = activity.get_htmlSummary();
            var pubDate = activity.get_published();
            var htmlContent = (activity.get_htmlContent() !== undefined) ? activity.get_htmlContent() : "";
            var lastReply = (activity.get_lastReplyTime() !== undefined) ? activity.get_lastReplyTime() : "";
            var repliesCount = (activity.get_repliesCount() !== undefined) ? activity.get_repliesCount() : 0;
            var repliesLink = activity.get_repliesLink();

            var output = displayTemplate.replace(/\{0\}/g, htmlSummary);
            output = output.replace(/\{1\}/, htmlContent);
            output = output.replace(/\{2\}/, pubDate);
            output = output.replace(/\{3\}/, repliesCount);
            output = output.replace(/\{4\}/, repliesLink);
            if (repliesCount > 0) {
                output += lastReplyTemplate.replace(/\{1}/, lastReply);
            }
            return output;
        }

    </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 />
        <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:
© 2015 Microsoft