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

Enumerating Contacts

Ff747875.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 contacts, load them into your in-memory collection (DataCollection), and then process the collection. The following code example demonstrates some of the required steps.

Ff747875.note(en-us,MSDN.10).gifNote:
The code example assumes that you already have a data context and an in-memory contact collection. For more information about the contact resource, see Working with Contacts (JavaScript Library). For more information about the data context, see Using the Data Context.
function contactsLoaded(dataLoadCompletedEventArgs) {
    $get('Name_list').options.length = 0; // Clear list of names.
    if (dataLoadCompletedEventArgs.get_resultCode() !== Microsoft.Live.AsyncResultCode.success) {
        alert("Contacts failed to load: " + Microsoft.Live.AsyncResultCode.toString(dataLoadCompletedEventArgs.get_resultCode()));
        return;
    }

    contactsCollection = dataLoadCompletedEventArgs.get_data();
    for (var i = 0; i < contactsCollection.get_length(); i++) {
        var contact = contactsCollection.getItem(i);
        addOption($get('Name_list'), contact.get_formattedName(), contact.get_id());
    }
    displayContact($get('Name_list').options[$get('Name_list').selectedIndex].value)
}

The following code sample shows how to enumerate contacts in a user contact collection. To create and test this Microsoft ASP.NET-based (.aspx) page:

  1. Follow the instructions in Getting Started Sample for ASP.NET.
  2. Add a new ASP.NET page to the project that you create in Getting Started Sample for ASP.NET.
  3. Replace the <html> element in the new ASP.NET page with the following code.
  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 runat="server">
    <title>Javascript API - Contact Enumerate</title>
    <script type="text/javascript" src="http://js.live.net/4.1/loader.js"></script>

    <script type="text/javascript">
        var dataContext;
        var contactsCollection;
        var contactSelected;

        function appLoaded(applicationLoadCompletedEventArgs) {
            Microsoft.Live.Core.Namespace.using("wl:Microsoft.Live");
            // Page refresh.
            if (wl.App.get_auth().get_state() === wl.AuthState.authenticated) {
                wl.App.get_dataContext().loadAll(Microsoft.Live.DataType.contacts, contactsLoaded);
            }
        }

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

        function signOutCallback() {
            $get('Name_list').options.length = 0;
            $get('txtBox1').value = "";
            $get('txtBox2').value = "";
            $get('txtBox3').value = "";
            $get('txtBox4').value = "";
        }
        function contactsLoaded(dataLoadCompletedEventArgs) {
            $get('Name_list').options.length = 0; // Clear list of names.
            if (dataLoadCompletedEventArgs.get_resultCode() !== Microsoft.Live.AsyncResultCode.success) {
                alert("Contacts failed to load: " + Microsoft.Live.AsyncResultCode.toString(dataLoadCompletedEventArgs.get_resultCode()));
                return;
            }

            contactsCollection = dataLoadCompletedEventArgs.get_data();
            for (var i = 0; i < contactsCollection.get_length(); i++) {
                var contact = contactsCollection.getItem(i);
                addOption($get('Name_list'), contact.get_formattedName(), contact.get_id());
            }
            displayContact($get('Name_list').options[$get('Name_list').selectedIndex].value)
        }
        // Add items to the drop down.
        function addOption(selectbox, text, value) {
            var optn = document.createElement("OPTION");
            optn.text = text;
            optn.value = value;
            selectbox.options.add(optn);
        }

        // For a contact selected in the dropdown, display its properties in the text boxes.
        function displayContact(id) {
            for (var i = 0; i < contactsCollection.get_length(); i++) {
                var contact = contactsCollection.getItem(i);
                if (contact.get_id() === id) {
                    contactSelected = contact;
                    $get('txtBox1').value = contactSelected.get_firstName();
                    $get('txtBox2').value = contactSelected.get_lastName();
                    $get('txtBox3').value = (contactSelected.get_cid() !== undefined) ? contactSelected.get_cid() : "";
                    $get('txtBox4').value = (contactSelected.get_isFriend() === true) ? "true" : "false";
                }
            }
        }
    </script>
    <style type="text/css">
    table { border-collapse: collapse;}
    table td{
    border: 1px solid blue;
    padding: 4px;}
    td.d span {
    display: inline-block;
    min-width: 120px;}
    </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_Contacts.View"
            onload="appLoaded">
        </wl:app>
        <h1>
           Listing Contact</h1>
               <wl:signin onsignin="{{signInCallback}}" onsignout="{{signOutCallback}}"/>
        <br />
        <hr />
        <table >
            <tr>
                <td>
                    <wl:userinfo />
                    <label>
                        Contact list:</label><br />
                    <br />
                    <select id="Name_list" onchange="displayContact($get('Name_list').options[$get('Name_list').selectedIndex].value)">
                        <option value="">Display Names</option>
                    </select>
                </td>
                <td class="d">
                    <span>FirstName: </span>
                    <input type="text" id="txtBox1" /><br />
                    <span >LastName: </span>
                    <input type="text" id="txtBox2" /><br />
                    <span>CID: </span>
                    <input type="text" disabled="disabled" id="txtBox3"/><br />
                    <span>Friend?:</span>
                    <input type="text" id="txtBox4" />
                </td>
            </tr>
        </table>
    </div>
</body>
</html>

Show:
© 2015 Microsoft