Handle Events for ContactManager (Lync 2010 SDK)
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 contact events for the group. 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, you 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 method manager_GroupAdded 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
A contact can be a member of multiple groups. If one of a contact's parent groups is removed, the contact may still appear in your contact list because of membership in another group.
SearchProviderStateChanged Event
The following example handles the search provider state changed event by checking the new status and if the search provider is synchronized, 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
Concepts
Walkthrough: Fill a Contact List (Lync 2010 SDK)
Walkthrough: Add, Remove, and Rename Custom Groups (Lync 2010 SDK)