Click to Rate and Give Feedback
MSDN
MSDN Library
Live Services SDK
 How to: Add or Remove a Contact

  Switch on low bandwidth view
How to: Add or Remove a Contact

You can give users the ability 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 enable a user to add a contact and can enable the user to preset which contact to add, the message that the contact will receive, the nickname for the contact, and the group that the contact will be placed in.

If you would like to let users update their contact list by using the Windows Live Messenger Library instead, you will 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 whom the user wants to add to the contact list. The recipient has the option to 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.
To get the ContactCollection instance for the current user
  • Use the get_contacts accessor function on the current user.

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

    _user.add_addContactCompleted(addContactCompleted);
To add a contact to the contact list
  • Call addContact on the current user.

    var address = "someone@example.com";
    _user.addContact(address, 'Invitation message.', null);
To remove a contact from the contact list
  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);

Description

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

Code

// 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 has been added (the displayContacts function is not included in this example).
function addContactCompleted(sender, e) 
{
    displayContacts();
}
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker