Edit

Share via


How to: Administer privacy relationships between Lync users

Learn how to set the privacy relationship between the signed-in Lync 2013 user and another Lync user by calling a single method in the Microsoft Lync 2013 API.

Applies to: Lync 2013 | Lync Server 2013

In this article
Prerequisites
Set a privacy relationship with a Lync contact
Sample application: Privacy Relationship Manager
Code examples: Set a privacy relationship
Additional resources

Prerequisites

The prerequisites for administering privacy relationships between Lync users are as follows:

Set a privacy relationship with a Lync contact

To set a privacy relationship

  1. Get the SIP URI strings of Lync users whom are selected in the UI contact list.

            /// <summary>
            /// Sets the selected privacy relationship level for all selected contacts
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void SetRelationship_Button_Click(object sender, RoutedEventArgs e)
            {
                //iterate on the collection of selected contact Uri strings from the 
                //UI ContactList control.
                foreach (string contactURI in myContactList.SelectedContactUris)
                {
                    SetPrivacyLevelOnAContact(contactURI);
                }
    
            }
    
  2. Get a Microsoft.Lync.Model.Contact object by calling the ContactManager.GetContactByUri method.

    The following example gets a Microsoft.Lync.Model.Contact object from the SIP URI of a selected user.

                //Get a contact object that represents the selected contact Uri
                Contact selectedContact = LyncClient.GetClient().ContactManager.GetContactByUri(contactURI);
    
  3. Check to see whether the state of the Contact object lets you set the privacy relationship by calling the Contact.CanChangeSetting method.

  4. Set the privacy relationship rule for the selected contact.

    The following example sets the privacy relationship rule by calling the Contact.BeginChangeSetting method.

            /// <summary>
            /// Sets the privacy relationship for a Lync user specified by URI
            /// </summary>
            /// <param name="contactURI">string. The SIP URI of a Lync user</param>
            private void SetPrivacyLevelOnAContact(string contactURI)
            {
                //Get a contact object that represents the selected contact Uri
                Contact selectedContact = LyncClient.GetClient().ContactManager.GetContactByUri(contactURI);
    
                //Check to see if the contact object state allows you to set the privacy level
                if (selectedContact.CanChangeSetting(ContactSetting.AccessLevel))
                {
                    //Set the privacy relationship contact setting
                    selectedContact.BeginChangeSetting(
                        ContactSetting.AccessLevel,
                        GetRadioChoice(),
                        (ar) =>
                        {
                            selectedContact.EndChangeSetting(ar);
                        },
                        null);
                }
            }
    

Sample application: Privacy Relationship Manager

The following example shows a sample application that lets a signed-in user set privacy relationships for any user in his or her contact list. The form contains a Microsoft.Lync.Controls.PresenceIndicator control to display the signed-in user’s presence and picture, a Microsoft.Lync.Controls.ContactList control to show the user’s contact list, a set of radio buttons that let the user select a privacy relationship rule to set, and a command button to set the selected privacy relationship for all contacts selected in the list.

Figure 1. Privacy relationship manager

Screen shot of privacy relationship manager UI

Code examples: Set a privacy relationship

The sample application shown in figure 1 is a WPF form with two Lync Controls: a ContactList control and a PresenceIndicator control. In addition, the UI shows a set of radio buttons representing privacy relationship rules, and a command button. The signed-in user expands a group on the contact list, selects one or more contacts, and select a privacy relationship rule. The event on the command button iterates on the selected contact SIP URI strings. For each string, it calls a helper method that sets the privacy relationship for the user represented by the SIP URI.

The following example declares the UI shown in figure 1.

<Window x:Class="PrivacyRelationship.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="clr-namespace:Microsoft.Lync.Controls;assembly=Microsoft.Lync.Controls"
    Title="Privacy Relationship Manager" 
        Height="Auto" 
        Width="Auto" 
        mc:Ignorable="d" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        d:DesignHeight="353" d:DesignWidth="602" SizeToContent="WidthAndHeight">
    <Grid>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="12,12,408,142">
            <!-- 
                Show the presence indicator. Hover over the icon to see the contact card.
                Set Source to a valid SIP URI in your organization. 
            -->
            <controls:PresenceIndicator 
                x:Name="Presence" 
                PhotoDisplayMode="Large" 
                />
            <!-- Use the DisplayName property from PresenceIndicator to show the user's name -->
            <TextBlock 
                Text="{Binding DisplayName, ElementName=Presence}" 
                Margin="4,0,0,0" 
                VerticalAlignment="Center"
                />
        </StackPanel>
        <StackPanel Height="281" HorizontalAlignment="Left" Margin="187,12,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="200">
            <controls:ContactList 
                Name="myContactList" 
                GroupViewBySetting="Relationship" 
                ContactLayoutView="OneLine" />
        </StackPanel>
        <StackPanel Height="100" HorizontalAlignment="Left" Margin="411,38,0,0" Name="stackPanel2" VerticalAlignment="Top" Width="124">
            <RadioButton Content="Friends and family" Height="16" Name="FriendsAndFamily_radio" />
            <RadioButton Content="Workgroup" Height="16" Name="Workgroup_radio" />
            <RadioButton Content="Collegues" Height="16" Name="Collegues_radio" />
            <RadioButton Content="External contacts" Height="16" Name="ExternalContacts_radio" />
            <RadioButton Content="Blocked" Height="16" Name="Blocked_radio" />
            <RadioButton Content="Default" Height="16" Name="Default_radio" />
        </StackPanel>
        <Button 
            Content="Set relationship" 
            Height="23" 
            HorizontalAlignment="Left" 
            Margin="411,144,0,0" 
            Name="SetRelationship_Button" 
            VerticalAlignment="Top" 
            Width="100" 
            Click="SetRelationship_Button_Click"/>
    </Grid>
 
</Window>

The following example declares a partial class for the UI shown in figure 1.

using System;
using System.Windows;
using Microsoft.Lync.Model;
 
namespace PrivacyRelationship
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        LyncClient _lyncClient;
        public Window1()
        {
            InitializeComponent();
            try
            {
                _lyncClient = LyncClient.GetClient();

            }
            catch (ClientNotFoundException) { MessageBox.Show("Lync is not running"); }
            catch (LyncClientException lce) { MessageBox.Show("Lync Client exception on GetClient " + lce.Message); }
        }
 
        /// <summary>
        /// Sets the selected privacy relationship level for all selected contacts
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SetRelationship_Button_Click(object sender, RoutedEventArgs e)
        {
            //iterate on the collection of selected contact Uri strings from the 
            //UI ContactList control.
            foreach (string contactURI in myContactList.SelectedContactUris)
            {
                SetPrivacyLevelOnAContact(contactURI);
            }
 
        }
 
        /// <summary>
        /// Sets the privacy relationship for a Lync user specified by URI
        /// </summary>
        /// <param name="contactURI">string. The SIP URI of a Lync user</param>
        private void SetPrivacyLevelOnAContact(string contactURI)
        {
            //Get a contact object that represents the selected contact Uri
            Contact selectedContact = _lyncClient.ContactManager.GetContactByUri(contactURI);
 
            //Check to see if the contact object state allows you to set the privacy level
            if (selectedContact.CanChangeSetting(ContactSetting.AccessLevel))
            {
                //Set the privacy relationship contact setting
                selectedContact.BeginChangeSetting(
                    ContactSetting.AccessLevel,
                    GetRadioChoice(),
                    (ar) =>
                    {
                        selectedContact.EndChangeSetting(ar);
                    },
                    null);
            }
        }
        /// <summary>
        /// Return an enumertor of AccessLevel corresponding to the radio button selected
        /// </summary>
        /// <returns></returns>
        private AccessLevel GetRadioChoice()
        {
            if (FriendsAndFamily_radio.IsChecked == true)
            {
                return AccessLevel.Friends;
            }
            if (Workgroup_radio.IsChecked == true)
            {
                return AccessLevel.Workgroup;
            }
            if (Collegues_radio.IsChecked == true)
            {
                return AccessLevel.Colleague;
            }
            if (ExternalContacts_radio.IsChecked == true)
            {
                return AccessLevel.External;
            }
            if (Blocked_radio.IsChecked == true)
            {
                return AccessLevel.Blocked;
            }
            if (Default_radio.IsChecked == true)
            {
                return AccessLevel.Default;
            }
            return AccessLevel.Invalid;
 
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //Set the PresenceIndicator source to the signed-in user URI
            Presence.Source = _lyncClient.Self.Contact.Uri;
         }
    }
}

See also