Managing a Contact Group

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Contacts can be categorized into groups. A group object implements the IMessengerGroup interface. A collection of groups is represented by an object implementing the IMessengerGroups interface. Managing a contact group amounts to manipulating these objects and interfaces as well as the appropriate methods and properties defined on the IMessenger2 interface. For example, an application calls the IMessenger2::MyGroups method to retrieve the collection of groups in Communicator.

Creating a Contact Group

An application creates a contact group by calling the IMessenger2::CreateGroup method. The following C# code snippet shows how to create a contact group.

CommunicatorAPI.Messenger communicator = ...

IMessengerGroup CreateGroup(string groupName)
{
    IMessengerGroup group = GetGroup(groupName);
    if (group == null)
        group = communicator.CreateGroup(groupName, communicator.MyServiceId) as IMessengerGroup;
    return group;
}

Finding a Contact Group

Finding a group involves enumerating the group collection to determine if any group matches some group attributes. The following C# code snippet shows how to find a group by name.

IMessengerGroup GetGroup(string groupName)
{
    IMessengerGroup group = null;
    foreach (IMessengerGroup g in communicator.MyGroups as IMessengerGroups)
    {
        if (g.Name == groupName)
        {
            group = g;
            break;
        }
    }
    return group;
}

Deleting a Contact Group

Deleting a contact group involves getting the specified group object and removing it from the group collection in Communicator, as illustrated in the following C# code snippet.

void RemoveGroup(string groupName)
{
    if (groupName == null || groupName == string.Empty)
        return;

    IMessengerGroup group = GetGroup(groupName);
    if (group == null)
        return;
    try
    {
        IMessengerGroups groups = communicator.MyGroups as IMessengerGroups;
        groups.Remove(group);
    }
    catch (Exception e)
    {
        if (e.Message.Contains("0x81000354"))
            MessageBox.Show("The specified group cannot be deleted.");
        else
            throw new Exception("Cannot delete group", e);
    }
}

Renaming a Contact Group

Renaming a group involves getting the specified group object and setting the IMessengerGroup::Name property of the group object with a new value.

void RenameGroup(string oldName, string newName)
{
    if (oldName == string.Empty || newName == string.Empty)
        return;

    IMessengerGroup group = GetGroup(oldName);
    if (group == null)
        return;

    group.Name = newName;
}

Adding a Contact to a Contact Group

Adding a contact to a contact group involves specifying the contact to be added to a group and the Contact Group itself. With the object implementing the IMessengerContact interface, you call IMessengerGroup::AddContact to associate the contact to the group. The concept is illustrated with the following code snippet.

object contactToAdd = null;
contactToAdd = communicator.GetContact(
                            "jaya@contoso.com",
                            communicator.MyServiceId
                            );
if (contactToAdd != null)
{
  IMessengerGroups theseGroups = (IMessengerGroups)communicator.MyGroups;
  foreach (IMessengerGroup thisGroup in theseGroups)
  {
    if (thisGroup.Name == "My Personal Contacts")
    {
      thisGroup.AddContact(contactToAdd);
      break;
    }
  }
}

See Also

Reference

Interfaces