Partager via


Handle Events for a Group (Lync 2010 SDK)

The collection of group and contact instances you get using these API calls reflect the current state of the contact list element of Lync 2010. A group event is raised when a user changes the name or membership of a group using either your application or Lync 2010. To keep your UI in synchronization with Lync 2010, you handle group events.

There are three events available for every group:

  • ContactAdded()

  • ContactRemoved()

  • NameChanged()

Register for Group Events

To register for events raised by a group, you include one line of code for each event. The following example registers the application for the ContactAdded() event of the group, designating the group_ContactAdded method as the method to be triggered by the event.

thisGroup.ContactAdded += group_ContactAdded;

If you want to stop receiving this event, then you add the following code.

thisGroup.ContactAdded -= group_ContactAdded;

Handle Group Events

Group events are raised when the state of a group is changed by updating the group membership or changing the name of the group.

Contact Added to Group

The ContactAdded() event is raised on the group when the state of the group is updated with the addition of a new contact. You can register for events on the contact added to the group in this event handler.

Lync 2010 maintains a subscription for all contacts in the local user’s contact list. For this reason, it is not necessary for your application to create and maintain a subscription for contacts added to any group in the group collection. In contrast, a contact you obtain using the search feature must be added to a subscription maintained by your application.

        void group_ContactAdded(Object source, GroupMemberChangedEventArgs data)
        {
            // Update application UI with event notification.
            System.Windows.Forms.MessageBox.Show(
                "New Contact in Group: " 
                + ((Group)source).Name 
                + " : " 
                + data.Contact.GetContactInformation(ContactInformationType.DisplayName).ToString());
                // Listen for contact events.
                data.Contact.SettingChanged += new System.EventHandler<Object, ContactSettingChangedEventArgs>(_Contact_ContactSettingChanged);
                data.Contact.ContactInformationChanged += new EventHandler<ContactInformationChangedEventArgs>(_Contact_OnInformationChanged);
                data.Contact.UriChanged += new EventHandler<UriChangedEventArgs>(c_UriChanged);
        }

Contact Removed from Group

The following example removes the registration for contact events on the contact removed from a group.

        void group_ContactRemoved(Object source, GroupMemberChangedEventArgs data)
        {
            // Update application UI with event notification.
            System.Windows.Forms.MessageBox.Show(
                "Contact was removed from Group: " 
                + ((Group)source).Name);
                data.Contact.ContactSettingChanged -= _Contact_ContactSettingChanged;
                data.Contact.ContactInformationChanged -= _Contact_OnInformationChanged;
                data.Contact.UriChanged -= c_UriChanged;
        }

Group Name Changed

The following example displays a message when the name of a group has changed.

   void group_NameChanged(Object source, GroupNameChangedEventArgs data)
        {
            System.Windows.Forms.MessageBox.Show("New Group name " + data.NewName);
        }

See Also

Concepts

Walkthrough: Fill a Contact List (Lync 2010 SDK)

Walkthrough: Add and Remove Contacts in a Group (Lync 2010 SDK)

Walkthrough: Add, Remove, and Rename Custom Groups (Lync 2010 SDK)

Handle Events for ContactManager (Lync 2010 SDK)