Click to Rate and Give Feedback
MSDN
MSDN Library
Online Services
Live Services SDK
 How to: Add or Remove a Contact
Collapse All/Expand All Collapse All
How to: Add or Remove a Contact

You can enable users to update their Windows Live Messenger contact list by adding and removing contacts.

By using the Contact List Control, a user can add a contact, remove a contact, and make changes to the group information for a contact. By using the Add Contact Control, a site can allow a user to add a contact and to preset the contact to add, the message that the contact receives, the nickname for the contact, and the group that the contact is placed in.

If you want to let users update their contact lists by using the Windows Live Messenger Library, you need to incorporate the following functionality into your application.

  • Get the ContactCollection instance for the current user.
  • Subscribe to AddContactCompletedEventHandler.
  • Create a function to add a contact to the contact list. In this example, the function is named addContact.
  • Create a function to remove a contact from the contact list. In this example, the function is named removeContact.
  • Add an event handler to run when AddContactCompletedEventHandler is fired. In this example, the event handler is named addContactCompleted.
Cc298449.note(en-us,MSDN.10).gifNote:
When addContact is called, an invitation is sent to the address of a contact that the user wants to add to the contact list. The recipient can accept or reject the invitation. If the recipient accepts, the contact is added to the user's contact list. If the recipient rejects the invitation, no further action is taken.
  • Use the get_contacts accessor function on the current user.

    _contactCollection = _user.get_contacts();
  • Call the add_addContactCompleted accessor function on the current user.

    _user.add_addContactCompleted(addContactCompleted);
  • Call addContact on the current user.

    var address = "someone@example.com";
    _user.addContact(address, 'Invitation message.', null);
  1. Locate the contact by using the following address string.

    var address = "someone@example.com";
    var contact = _contactCollection.find(address, Microsoft.Live.Messenger.IMAddressType.windowsLive);
  2. Call removeContact on the ContactCollection instance for the current user.

    _contactCollection.remove(contact);

The following example shows the code for managing contacts. A valid user sign-in session is assumed.

// Get the contact collection for the current user.
_contactCollection = _user.get_contacts();
// Subscribe to addContactCompleted.
_user.add_addContactCompleted(addContactCompleted);
// Add a new contact to the contact list.
function AddNewContact() 
{
    var address = document.getElementById("txtContact").value;
    if (_user)
    {
        _user.addContact(address, "Invitation message.", null);
    }
}

// Call a function to refresh the contact list when a new contact is added (the displayContacts function is not included in this example).
function addContactCompleted(sender, e) 
{
    displayContacts();
}
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker