Adding and Removing Contacts (JavaScript Library)

Ff749357.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.

You can enable users to update their 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 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.

To work with contact lists programmatically, 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 AddNewContact.
  • 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.
Ff749357.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.
To get the ContactCollection instance for the current user
  • Use the get_contacts function on the current user instance.

    contactCollection = user.get_contacts();
    
To subscribe to AddContactCompletedEventHandler
  • Call the add_addContactCompleted function on the current user instance.

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

    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 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 how to work with contacts programmatically.

// 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 = "someone@example.com";
    if (user)
    {
        user.addContact(address, "Invitation message.", null);
    }
}

// Remove a contact from your contact list.
function RemoveContact()
{
   var address = someone@example.com;
   var contact = contactCollection.find(address, Microsoft.Live.Messenger.IMAddressType.windowsLive);
   contactCollection.remove(contact);
}

// Call when addContactCompleted event is fired.
function addContactCompleted(sender, e) 
{
    Sys.Debug.trace("addContactCompleted called.");
}
Show: