Handle Events for a Contact (Lync 2010 SDK)

There are three kinds of state change events that are raised on an individual contact. These events can only be raised when the contact has been added to a ContactSubscription and your application has registered for events on a Contact. In the case where a given contact appears in the Lync 2010 contact list, you do not need to add the contact to a contact subscription because Lync 2010 itself maintains a subscription. In contrast, a contact that you obtain using the search feature of the API must be added to a contact subscription that your application maintains.

  • The ContactInformationChanged event is raised when the contact has published an update to contact information or published new contact information. You must catch this event to keep current contact information for a contact. At a minimum, you should maintain the current ContactInformationType. Availability. The Availability information type tells you if a contact is on-line, busy, in a meeting, in do-not-disturb status, or off-line.

  • The SettingChanged event is raised when your client has changed a setting value for a contact. This event is raised when a user changes the access level or tagging attribute of a contact.

  • The UriChanged event is raised when the URI has changed for a remote user contact.

Contact Events

The following sections provide examples that handle the three kinds of contact events.

Contact Information Changed

The following example handles the event raised when contact information is updated. It is important to note that this example queries the data parameter property, ChangedContactInformation for only the Availablility type. If the collection of ContactInformationType enumerators contains this type, then the corresponding value has been updated. To get the updated value, the example queries the source parameter as an instance of Contact for the updated value itself.

        /// <summary>
        /// Handles event raised when contact contact information item collection has been updated.
        /// </summary>
        /// <param name="source">object. Contact whose information has been updated.</param>
        /// <param name="data">ContactInformationChangedEventArgs. The contact information that changed.</param>
        /// <remarks>This callback is used to update the public contactModel card string class property ContactCardInformation.
        /// The event data parameter exposes a member property, ChangedPresenceItems. This property is a list of ContactInformationType. The types in 
        /// the list are the presence item types whose change resulted in this state change event.
        /// </remarks>
        void _Contact_OnInformationChanged(Object source, ContactInformationChangedEventArgs data)
        {
            try
            {
                string newCard = string.Empty;
                StringBuilder sb = new StringBuilder();
                if (data.ChangedContactInformation.Contains(ContactInformationType.Availability))
                {
                    object availability = ((Contact)source).GetContactInformation(ContactInformationType.Availability);
                    object activityString = ((Contact)source).GetContactInformation(ContactInformationType.Activity);
                    if (availability != null)
                    {
                        sb.Append(availability.ToString() + " " + activityString.ToString());
                        newCard = "Updated availability for "
                        + ((Contact)source).GetContactInformation(ContactInformationType.DisplayName).ToString()
                        + System.Environment.NewLine
                        + sb.ToString();
                    }
                }
                
            }
            catch (System.IO.InvalidDataException x)
            {
                WinForm.MessageBox.Show("Invalid Data Exception, Contact.ContactInformationChanged " + x.Message);
            }
        }

Contact Setting Changed

The following example handles a contact property changed event by getting the type of property that changed and the new value. The result is displayed to a user in a MessageBox.

        /// <summary>
        /// Event handler for ContactModel event raised when a setting on a contactModel has changed.
        /// </summary>
        /// <param name="source">Contact. The ContactModel instance whose inner Contact setting has changed.</param>
        /// <param name="data">ContactSettingChangedEventArgs. Event data.</param>
        private void ContactModel_ContactSettingChanged(object source, ContactSettingChangedEventArgs data)
        {

            string ChangedProperty = string.Empty;
            switch (data.Setting)
            {
                case ContactSetting.AccessLevel:
                    ChangedProperty = ((Contact)source).GetContactInformation(ContactInformationType.DisplayName).ToString() + " access level changed " + data.Value.ToString();
                    break;
                case ContactSetting.DefaultContactEndpoint:
                    ChangedProperty = ((Contact)source).GetContactInformation(ContactInformationType.DisplayName).ToString() + " default endpoint changed " + data.Value.ToString();
                    break;
                case ContactSetting.ExchangeServiceEntryId:
                    ChangedProperty = ((Contact)source).GetContactInformation(ContactInformationType.DisplayName).ToString() + " Exchange service Entry Id " + data.Value.ToString();
                    break;
                case ContactSetting.Source:
                    ChangedProperty = ((Contact)source).GetContactInformation(ContactInformationType.DisplayName).ToString() + " Sources changed " + data.Value.ToString();
                    break;
                case ContactSetting.Tagged:
                    ChangedProperty = ((Contact)source).GetContactInformation(ContactInformationType.DisplayName).ToString() + " Tagging changed " + data.Value.ToString();
                    break;
            }
            MessageBox.Show("Contact Setting Changed: " + ChangedProperty);
        }

Contact Uri Changed

The following example handles the event raised when a contact's Uri has changed. The example uses a MessageBox to alert a user of the change.

        /// <summary>
        /// Alerts user when a Contact Uri has changed
        /// </summary>
        /// <param name="source">object. Contact whose Uri has changed</param>
        /// <param name="data">UriChangedEventArgs Event data</param>
        void _Contact_OnUriChanged(Object source, UriChangedEventArgs data)
        {
            System.Windows.Forms.MessageBox.Show(((Contact)source).GetContactInformation(ContactInformationType.DisplayName).ToString() + " uri changed to " + data.NewUri);
        }

See Also

Concepts

Walkthrough: Fill a Contact List (Lync 2010 SDK)

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