Adding and Removing Contact Groups from the List

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.

The example contact list allows you to manage your contact groups as well as contacts themselves. In addition to the default "All Contacts" group that is always a part of a contact list, the local user has the ability to define his own groups. These user-defined groups can be removed from the list as well. Removing a user-defined contact group from the contact list does not remove the contacts themselves. The individual contacts are placed in the "Other Contacts" group when their parent user-defined group is removed.

Adding a Contact Group

The following example method demonstrates the adding of a contact group to the example contact list. The sample application contains a Windows form class called stringInputForm. This form is used to collect the user-supplied name of the new group. Notice that the code does not attempt to rebuild the contact list after calling the IMessenger2::CreateGroup method. This is because the custom application has registered an event handler for the DMessengerEvents::OnGroupAdded event. When the Office Communicator server has notified the local client that the group has been successfully created, this event handler calls a method on the contactList class that rebuilds the contact list.

private void addGroupToolStripMenuItem_Click(object sender, EventArgs e)
{
  try
  {
    stringForm = new stringInputForm();
    stringForm.PromptString = "Group Name";
    stringForm.TitleText = "Input Group Name";
    stringForm.ShowDialog();
    if (stringForm.StringBoxText.Length > 0)
       communicator.CreateGroup(stringForm.StringBoxText, "serviceID");
    stringForm.Close();
  }
  catch (COMException CGCE)
  {
    MessageBox.Show(formReturnErrors.returnComError(CGCE.ErrorCode));
  }
  catch (Exception CGE)
  {
    MessageBox.Show(CGE.Message.ToString());
  }
}

Removing a Contact Group

The following code example demonstrates how to remove a user-defined contact group from the custom contact list by calling the IMessengerGroups::Remove method. The contact list is not rebuilt by this method. The sample application has registered an event handler for the DMessengerEvents::OnGroupRemoved event. This event handler is responsible for rebuilding the list after being notified by the Office Communicator server that the user-defined group has been successfully deleted.

private void removeGroupToolStripMenuItem_Click_1(object sender, EventArgs e)
{
  try
  {
    ListViewItem selectedContactItem = contactListView.FocusedItem;
    if (selectedContactItem != null)
    {
      IMessengerGroups theseGroups = (IMessengerGroups)communicator.MyGroups;
      foreach (IMessengerGroup thisGroup in theseGroups)
      {
        if (thisGroup.Name == selectedContactItem.Group.Header)
        {
           theseGroups.Remove(thisGroup);
           break;
        }
      }
    }
  }
  catch (COMException ICE)
  {
                MessageBox.Show(formReturnErrors.returnComError(ICE.ErrorCode));
  }
}

See Also

Concepts

Building a Custom Contact List