How to: Publish enhanced presence information

Learn how to publish Microsoft Lync 2013 presence information such as the availability and personal note of a Lync 2013 contact by using methods in Microsoft Lync 2013 SDK.

The prerequisites for publishing enhanced presence information are as follows:

  • Microsoft Lync 2013 must be installed and running on the development computer.

  • You must have sign-in credentials for Microsoft Lync Server 2013.

  • Microsoft Lync 2013 SDK must be installed on the development computer.

Core concepts to know

Topic

Description

Enhanced presence content in Lync SDK

Describes how Microsoft Lync 2013 enhances the industry standard SIP simple presence.

Presence publication and subscription in Lync SDK

Describes what it means to publish and subscribe to enhanced presence in Lync 2013.

This process involves defining an array of contact information types and a Dictionary<PublishableContactInformationType, object> that holds the contact information to be published. Publishing a set of contact information items raises the ContactInformationChanged event on the publishing contact. If the publishing contact is the local user, the event is raised both locally and for remote users who have subscribed to the local contact.

The following illustration shows the classes, methods, and events used in the process of publishing contact information.

OCOM_Self_Publish

To publish enhanced presence information

  1. Get the LyncClient instance and verify that the client is signed in to the server.

    For information about signing in to Microsoft Lync Server 2013, see How to: Sign a user in to Lync.

  2. Create a Dictionary<PublishableContactInformationType, object> of contact information types and the corresponding values to be updated.

    The Dictionary you declare and instantiate is passed into BeginPublishContactInformation.

  3. Add a ContactInformationType and the corresponding publishable value to the dictionary you created previously.

  4. Read the Self property to get an instance of Self.

  5. Optional: Declare and instantiate a state object such as a string and fill it with appropriate state information.

    The callback method you provide should access this state information to provide a context for the operation.

  6. Call BeginPublishContactInformation, passing the dictionary, the callback method (or null), and the state object.

The following examples publish the presence information typically published in a custom application.

Publish a personal note and current availability

The following example method publishes a new personal note for the local user.

Tip Tip

In the example, a callback method is included as a parameter in the PublishPresenceItems method call. You should pass a null value in the callback parameter position if you are not interested in catching the result of the publication.

        /// <summary>
        /// Publishes an update to a personal note
        /// </summary>
        /// <param name="newNote">string. The new personal note text.</param>
        public void PublishPersonalNoteAndFreeAvailability(string newNote)
        {
            //Each element of this array must contain a valid enumeration. If null array elements 
            //are passed, an ArgumentException is raised.
            Dictionary<PublishableContactInformationType, object> publishData = new Dictionary<PublishableContactInformationType, object>();
            publishData.Add(PublishableContactInformationType.PersonalNote, newNote);
            publishData.Add(PublishableContactInformationType.Availability, ContactAvailability.Free);

            //Helper method is found in the next example.
            SendPublishRequest(publishData, "Personal Note and Availability");
        }


The following example begins the asynchronous publication process.

        /// <summary>
        /// Sends a publication request and handles any exceptions raised.
        /// </summary>
        /// <param name="publishData">Dictionary. Information to be published.</param>
        /// <param name="PublishId">string. Unique publication identifier.</param>
        private void SendPublishRequest(
           Dictionary<PublishableContactInformationType, object> publishData,
            string PublishId)
        {

            try
            {
                _LyncClient.Self.BeginPublishContactInformation(
                    publishData,
                    (ar) => {
                        _LyncClient.Self.EndPublishContactInformation(ar);
                    },
                    null);
            }
            catch (COMException ce)
            {
                MessageBox.Show("publish COM exception: " + ce.ErrorCode.ToString());
            }
            catch (ArgumentException ae)
            {
                MessageBox.Show("publish Argument exception: " + ae.Message);
            }
        }


The following example handles the ContactInformationChanged event that is raised when the current contact information state changes. The example uses a message box to notify a user that presence has been updated and then removes the registration for Contact information update events.

        /// <summary>
        /// Handles event raised when the presence of a contact has been updated
        /// </summary>
        /// <param name="source">Contact. Contact instance whose presence is updated</param>
        /// <param name="data">PresenceItemsChangedEventArgs. Collection of presence item types whose values have been updated.</param>
        void Contact_ContactInformationChanged(object sender, ContactInformationChangedEventArgs e)
        {
            if (((Contact)source) == _LyncClient.Self.Contact)
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.Append("Changed types: " + System.Environment.NewLine);
                foreach (ContactInformationType changedInformationType in e.ChangedContactInformation)
                {
                    sb.Append(changedInformationType.ToString() + System.Environment.NewLine);
                }
                MessageBox.Show("Self publish succeeded: " + sb.ToString());
            }
        }

Reset availability to default contact availability

To reset a user’s current availability to the default availability calculated by the Lync 2013 client, publish ContactAvailability.None.

        /// <summary>
        /// Publishes default client availability
        /// </summary>

        public void PublishDefaultAvailability()
        {
            //Each element of this array must contain a valid enumeration. If null array elements 
            //are passed, an ArgumentException is raised.
            Dictionary<PublishableContactInformationType, object> publishData = new Dictionary<PublishableContactInformationType, object>();
            publishData.Add(PublishableContactInformationType.Availability, ContactAvailability.None);

            //Helper method is found in the next example.
            SendPublishRequest(publishData, "Reset availability to default");
        }


Show:
© 2014 Microsoft