How to: Change the privacy relationship between Lync users

Learn about how to programmatically change the privacy relationship between two Microsoft Lync 2013 contacts by using Microsoft Lync 2013 SDK.

Applies to: Lync 2013 | Lync Server 2013

In this article
Prerequisites
Privacy relationship options
Change the privacy relationship of a contact
Code example: Accepting parameters
Additional resources

Prerequisites

The prerequisites for using presence information in an application 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.

Privacy relationship options

The following table presents the publishable contact information types that are available to contacts assigned to the five privacy access levels.

Types/relationship

Friend

Workgroup

Colleague

External

Blocked

Phones

Mobile, home, and any other published phone numbers.

None.

Contact information

Display name, email address, job title, display photo, company, office location, and work phone number.

Display name, email address, job title, company, and display photo.

Display name, email address, company, job title, and display photo.

Display name, email address, company, job title, and display photo.

Voicemail

Voicemail URL when the user is enabled for Lync.

Voicemail URL when the user is enabled for Lync.

Voicemail URL when the user is enabled for Lync.

Voicemail URL when the user is enabled for Lync.

Working calendar

The working-hour blocks as specified in the user’s calendar.

The working-hour blocks as specified in the user’s calendar.

The working-hour blocks as specified in the user’s calendar.

Free/busy calendar

The time periods when the user is free or busy according to the user’s calendar.

The time periods when the user is free or busy according to the user’s calendar.

The time periods when the user is free or busy according to the user’s calendar.

Notes

Presence note or OOF message from the user's Outlook account.

Presence note or OOF message from the user's Outlook account.

Presence note or OOF message from the user's Outlook account.

Presence note or OOF message from the user's Outlook account.

Change the privacy relationship of a contact

To change the privacy relationship of a contact

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

    For information, see How to: Sign a user in to Lync.

  2. Get the Contact instance to be updated.

    Any valid contact, regardless of its source, can be updated to change the privacy relationship setting. For information about getting the contacts in a user’s contact list, see How to: Display a contact list.

  3. Call the Contact.BeginChangeSetting method, specifying that the privacy relationship is to be set by setting the ContactSetting.AccessLevel and by an AccessLevel enumerator representing the new privacy relationship.

  4. Catch the System.IAsyncResult returned from the call.

  5. Call Contact.EndChangeSetting to complete the operation.

    TipTip

    If you do not want to block execution on your UI thread while the operation runs, pass a System.AsyncCallback method into BeginChangeSetting and make the EndChangeSetting call inside the callback.

Code example: Accepting parameters

The following example accepts two string parameters representing the URI of a user and the new access level desired. The access level string is converted to an AccessLevel enumerator before an instance of Contact is obtained from ContactManager by calling into GetContactByUri.

The contact setting, ContactSetting.AccessLevel, is set by calling into Contact.BeginChangeSetting and passing both an enumerator for the property to be updated and an enumerator for the new property value.

        /// <summary>
        /// Updates the privacy relationship of a contact specified by Uri
        /// </summary>
        /// <param name="ContactUri">string. Uri of contact to update.</param>
        /// <param name="newAccessLevel">string. New privacy relationship.</param>
        public void UpdatePrivacyRelationship(string ContactUri, string newAccessLevel)
        {
            AccessLevel newLevelEnumerator = AccessLevel.Default;
            switch (newAccessLevel.ToUpper().Trim())
            { 
                case "FRIENDS":
                    newLevelEnumerator = AccessLevel.Friends;
                    break;
                case "WORKGROUP":
                    newLevelEnumerator = AccessLevel.Workgroup;
                    break;
                case "COLLEAGUE":
                    newLevelEnumerator = AccessLevel.Colleague;
                    break;
                case "EXTERNAL":
                    newLevelEnumerator = AccessLevel.External;
                    break;
                case "BLOCKED":
                    newLevelEnumerator = AccessLevel.Blocked;
                    break;
            }
            Contact contactToUpdate = _LyncClient.ContactManager.GetContactByUri(ContactUri);
            if (contactToUpdate != null)
            {
                contactToUpdate.BeginChangeSetting(ContactSetting.AccessLevel, newLevelEnumerator, SetPrivacyCallback, contactToUpdate);
            }
        }

        /// <summary>
        /// Handles async callback when set privacy operation completes
        /// </summary>
        /// <param name="ar">IAsyncResult. The state of the operation.</param>
        private void SetPrivacyCallback(IAsyncResult ar)
        {
            if (ar.IsCompleted == true)
            {
                ((Contact)ar.AsyncState).EndChangeSetting(ar);
                Console.WriteLine("Privacy relationship updated for " + ((Contact)ar.AsyncState).GetContactInformation(ContactInformationType.DisplayName).ToString());
            }
        }

See also