In Office Communicator Automation API, a contact is represented by an object implementing the IMessengerContact interface and a contact list is represented by an object implementing the IMessengerContacts interface. Managing a contact means finding a contact (see Building a Custom Contact List), adding a user to or removing a user from the contact list (see below), and maintaining contacts in groups.

Adding a User to Contact List

There are two ways to add a user to the caller's contact list in a Communicator application. The first one uses the IMessenger::AddContact method. This method starts Add a Contact Wizard for adding a user to the contact list. It is equivalent to clicking Add a Contact on the Contacts menu in the Communicator window. The second one uses the IMessengerPrivate::AddContact method.

This method bypasses the wizard and adds the contact without any user intervention.

The following C# code snippet illustrates how to add a user to the contact list of the caller.

Copy Code
CommunicatorAPI.Messenger communicator = null;
CommunicatorPrivate.MessengerPrivClass communicatorPrivate = null


void AddContact(string userId, bool useWizard)
{
   if (useWizard)
	communicator.AddContact(0, userId);
   else
	communicatorPrivate.AddContact(userId, communicator.MyServiceId);
}

Finding a Contact or a User

Finding a user involves calling the IMessenger::GetContact method and specifying the user ID as an input parameter. If the user is in the contact list of the caller, the application can get the user object off the connected Communicator instance. If the user is not in the caller's contact list, the application must retrieve the user from the Session Initiation Protocol (SIP) provider of Communicator that is the Communications Server. These are illustrated in the following C# code snippet.

Copy Code
private IMessengerContact FindContact(string userID)
{
	IMessengerContact contact = null;
	// Try the local contact list first
	try
	{
		contact = (IMessengerContact)communicator.GetContact(userID, "");
}
	catch
	{
		contact = null;
}

	// For a nonlocal contact, try the SIP Provider of Communicator
	if (contact == null || contact.Status == MISTATUS.MISTATUS_UNKNOWN)
	{
		try
		{
			contact =
				(IMessengerContact)communicator.GetContact(userID,
				communicator.MyServiceId);
			return contact;
	}
		catch
		{
			contact = null;
			return contact;
	}
}
	else
	{
		return contact;
}
}

Removing a Contact from the Contact List in Communicator

To remove a contact from the caller's contact list, an application can call the IMessengerContacts::Remove method. The following C# code snippet illustrates how to remove a contact from the contact list of the caller.

Copy Code
void RemoveContact(string signinName)
{
	IMessengerContact contact = communicator.GetContact(signinName, "") as IMessengerContact;
	IMessengerContacts contacts = communicator.MyContacts as IMessengerContacts;
	contacts.Remove(contact);
}

See Also