Editar

Compartir a través de


Handle events for ContactManager

Beyond the basics topic

Learn about handling the events raised by the Microsoft.Lync.Model.ContactManager object in Microsoft Lync 2013 SDK.

Applies to: Lync 2013 | Lync Server 2013

In this article
Event handling overview
Register for contacts and groups manager events
Handle contacts and groups manager events
Additional resources

Event handling overview

Event handlers triggered by events raised in the contacts and groups manager are a good place to stage the update of a contact list displayed in your user interface. In these event handlers, you can also register for or remove registration for the group’s contact events. For example, when a group is added, it triggers a GroupAdded event. The event handler for this event should register for the ContactAdded event and ContactRemoved event for this new group.

There are three events available for ContactManager:

Register for contacts and groups manager events

To register for events raised by the contacts and groups manager, include one line of code for each event. The following example registers the application for the GroupAdded event of the contacts and groups manager, designating the manager_GroupAdded method as the method to be triggered by the event.

_LyncClient.ContactManager.GroupAdded += new EventHandler<Object, GroupCollectionChangedEventArgs>(manager_GroupAdded);

Handle contacts and groups manager events

The following examples handle the events raised when a group is added or removed from the contacts and groups manager.

GroupAdded event

The following example handles the GroupAdded event by registering for events on the group.

   /// Handler for event raised when a group is added.
        /// <param name="source">Object. The ContactManager instance that raised the event.</param>
        /// <param name="data">GroupCollectionChangedEventArgs. The event state object.</param>
        void manager_GroupAdded(Object source, GroupCollectionChangedEventArgs data)
        {
            if (data.Group.Name.Length > 0)
            {
                data.Group.ContactAdded += new EventHandler<Object, GroupMemberChangedEventArgs>(group_ContactAdded);
                data.Group.ContactRemoved += new EventHandler<Object, GroupMemberChangedEventArgs>(group_ContactRemoved);
                data.Group.NameChanged += new EventHandler<Group, GroupNameChangedEventArgs>(group_NameChanged);

                // Update UI to add group and contacts in the added group.
            }
        }

GroupRemoved event

The following example handles the GroupRemoved event by removing the registration for group events on the group and removing the group from the local group Dictionary<string, Group>.

   /// Handler for event raised when a group is deleted.
        /// <param name="source">ContactsAndGroupsManager event source</param>
        /// <param name="data">GroupCollectionEventData event data</param>
        void manager_GroupRemoved(ContactsAndGroupsManager source, GroupCollectionEventArgs data)
        {
            if (data.Group != null)
            {
                // Remove registration for group events on removed group.
                data.Group.ContactAdded -= group_ContactAdded;
                data.Group.ContactRemoved -= group_ContactRemoved;
                data.Group.NameChanged -= group_NameChanged;

                // Update UI to remove group and contacts in group.
            }
        }
Important noteImportant

A contact can be a member of multiple groups. If one of a contact's parent groups is removed, the contact might still appear in your contact list because of its membership in another group.

SearchProviderStateChanged event

The following example handles the search provider state changed event by checking the new status. If the search provider is synchronized, it notifies the user. In practice, your application should update a UI element to let a user choose the updated search provider before starting a new contact search.

        /// <summary>
        /// Handles the event raised on the ContactManager when a search provider status has changed.
        /// </summary>
        /// <param name="sender">object. the ContactManager that raised the event.</param>
        /// <param name="e">SearchProviderStateChangedEventArgs. The event state.</param>
        void _ContactManager_SearchProviderStateChanged(object sender, SearchProviderStateChangedEventArgs e)
        {
            if (e.NewStatus == SearchProviderStatusType.SyncSucceeded)
            {
                System.Windows.Forms.MessageBox.Show(e.Provider.ToString()
                    + " is now " + e.NewStatus.ToString()
                    + System.Environment.NewLine
                    + "You can conduct contact searches using this provider");
            }
            else if (e.NewStatus == SearchProviderStatusType.SyncSucceededForInternalOnly)
            {
                System.Windows.Forms.MessageBox.Show(e.Provider.ToString()
                    + " is now " + e.NewStatus.ToString()
                    + System.Environment.NewLine
                    + "You can conduct organization contact searches using this provider");
            }
            else if (e.NewStatus == SearchProviderStatusType.SyncSucceededForExternalOnly)
            {
                System.Windows.Forms.MessageBox.Show(e.Provider.ToString()
                    + " is now " + e.NewStatus.ToString()
                    + System.Environment.NewLine
                    + "You can conduct public contact searches using this provider");
            }

        }

See also