Export (0) Print
Expand All

Listing Contact Activities

Ff752399.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 user contact activities. Contact activities are the activities of an authenticated user's contacts in the current application or webpage to which the user is signed in, along with contact activities created by Windows Live that are public according the contact’s privacy settings. Activities created by other applications are not part of the fetched collection.

Ff752399.note(en-us,MSDN.10).gifNote:
Contact activities are different than the activities of an authenticated user. For information about how to list an authenticated user's activities, see Listing User Activities.

Activities, like contacts and profiles, are requested through the data context, which is available after a user has signed in and consented. The specific consent scope for user contact activities is the contactsActivities field.

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

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

In the preceding example, the collection of activities is in the variable contactsActivitiesCollection. 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 limit the amount of information that is displayed at one time, you can specify query parameters when you load the activities from the data context. You can also use a View object to page and filter the displayed results. The following code example demonstrates how to load the activities of the contacts of the currently authenticated user, request only activities that have the word "photo" in the title, and show only the five most recent activities. Note that the load method is used instead of the loadAll method. (A full sample is shown later in this topic.)

function getActivities() {
    var options = { "$filter": "Title contains 'photo'", "$top" : "5"};
    Microsoft.Live.App.get_dataContext().load(Microsoft.Live.DataType.contactsActivities, options, contactsActivitiesLoaded);
}
function contactsActivitiesLoaded(dataLoadCompletedEventArgs) {
    var contactsActivitiesCollection = dataLoadCompletedEventArgs.get_data();
    for (var i = 0; i < contactsActivitiesCollection.get_length(); i++)
        var activity = contactsActivitiesCollection.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 page that lists the activities of a user's contacts. 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 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>Contact Activities List</title>
    <script type="text/javascript" src="http://js.live.net/4.1/loader.js"></script>
    <script type="text/javascript">
        var dataContext;
        var contactsActivitiesCollection;

        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: " + Microsoft.Live.AsyncResultCode.toString(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.contactsActivities, options, contactsActivitiesLoaded);
        }
        function contactsActivitiesLoaded(dataLoadCompletedEventArgs) {
            if (dataLoadCompletedEventArgs.get_resultCode() !== Microsoft.Live.AsyncResultCode.success) {
                alert("Activities failed to load! Are you signed in?");
                return;
            }
            contactsActivitiesCollection = dataLoadCompletedEventArgs.get_data();
            $get('ActivityCount').innerHTML = contactsActivitiesCollection.get_length();
            createListOfActivities(contactsActivitiesCollection);

        }
        // List just "view" of 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 />
        Number of activities loaded: <span id="ActivityCount"></span>
        <br />
        <h3>Recent Contact 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:
© 2014 Microsoft