Creating and Using a Lync WPF Control: Code Listing (Part 2 of 2)

Summary:   Part 2 contains a code listing for the application described in part 1, and a conclusion.

Applies to:   Microsoft Lync 2010 SDK

Published:   March 2012 | Provided by:   John Austin and John Clarkson, Microsoft | About the Author


The following example shows the user control library .xaml file. This code creates three buttons that start Click event handlers in the code behind, and a list box to display URIs.

<UserControl x:Class="WpfUserControlLibraryLync1.UserControl1"
             d:DesignHeight="300" d:DesignWidth="300">
        <Button Click="button1_Click" Content="Create Group" Height="23" HorizontalAlignment="Left" Margin="12,29,0,0" Name="button1" VerticalAlignment="Top" Width="108" />
        <Button Click="button2_Click"  Content="List contacts" Height="23" HorizontalAlignment="Left" Margin="12,82,0,0" Name="button2" VerticalAlignment="Top" Width="108" />
        <Button Click="button3_Click" Content="Send invites" Height="23" HorizontalAlignment="Left" Margin="12,135,0,0" Name="button3" VerticalAlignment="Top" Width="108" />
        <ListBox Height="100" HorizontalAlignment="Left" Margin="150,58,0,0" Name="listBox1" VerticalAlignment="Top" Width="120" SelectionMode="Multiple" />

The following example shows the content of the user control library code behind file. This code creates a new user group in the Lync client, displays URIs of group members in a list box, and sends an instant message to the selected URIs. For more information, see the Introduction in Creating and Using a Lync WPF Control: How to (Part 1 of 2).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Lync.Model;
using Microsoft.Lync.Model.Group;
using Microsoft.Lync.Model.Extensibility;
using Microsoft.Lync.Model.Conversation;

namespace WpfUserControlLibraryLync1
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl
        ContactManager contactManager;
        LyncClient client;

        // Class constructor.
        public UserControl1()
            client = LyncClient.GetClient();
            contactManager = client.ContactManager;
            listBox1.SelectionMode = SelectionMode.Multiple;

        #region Click event handlers

        // Event handler adds a "Sample" group.
        private void button1_Click(object sender, RoutedEventArgs e)
            string groupName = "Sample";
            contactManager.GroupAdded += new EventHandler<GroupCollectionChangedEventArgs>(contactManager_GroupAdded);
            AsyncCallback callback = new AsyncCallback(BeginAddGroupComplete);
            contactManager.BeginAddGroup(groupName, BeginAddGroupComplete, null);

        // Event handler fills a contact list.
        private void button2_Click(object sender, RoutedEventArgs e)
            foreach (Group _Group in contactManager.Groups)
                if (_Group.Type == GroupType.CustomGroup)

        // Event handler sends conference invitations.
        private void button3_Click(object sender, RoutedEventArgs e)
            Automation automation = LyncClient.GetAutomation();

            // Add contact URIs to a List object.
            List<string> inviteeList = new List<string>();
            for (int i = 0; i < listBox1.SelectedItems.Count; i++)
            { inviteeList.Add(listBox1.SelectedItems[i].ToString()); }

            // Create a settings object.
            Dictionary<AutomationModalitySettings, object> settings = new Dictionary<AutomationModalitySettings, object>();

            // Specify message modality.
            AutomationModalities mode = AutomationModalities.InstantMessage;

            // Add settings to the settings object.
            settings.Add(AutomationModalitySettings.FirstInstantMessage, "Weekly project status conference is starting...");
            settings.Add(AutomationModalitySettings.SendFirstInstantMessageImmediately, true);

            // Launch the conference invite.
            IAsyncResult ar = automation.BeginStartConversation(
                , inviteeList
                , settings
                , null
                , null);

            // Get the conversation object.
            ConversationWindow window = automation.EndStartConversation(ar);
            Conversation conference = window.Conversation;

            // Display the conference URI.
            textBox1.Text = "conference URI: " + conference.Properties[ConversationProperty.ConferencingUri] + "?" + conference.Properties[ConversationProperty.Id];

        // Event handler simulates sending email containing the conference URI.
        private void button4_Click(object sender, RoutedEventArgs e)
            MessageBox.Show("sending email with conference URI");


        #region other event handlers

        // Event handler for the GroupAdded event.
        // Used for debugging only.
        void contactManager_GroupAdded(object sender, GroupCollectionChangedEventArgs e)
            MessageBox.Show("GroupAdded event occurred");

        // Event handler for the ContactAdded event.
        // Used for debugging only.
        void group_ContactAdded(object sender, GroupMemberChangedEventArgs e)
            MessageBox.Show("ContactAdded event occurred");


        // Adds contacts to the list box.
        private void GetGroupContacts(Group group)
            ContactSubscription newSubscription = contactManager.CreateSubscription();
            Dictionary<ContactInformationType, object> _ContactInformation = new Dictionary<ContactInformationType, object>();

            group.ContactAdded += new EventHandler<GroupMemberChangedEventArgs>(group_ContactAdded);

            // Iterate on the contacts in the group.
            // For each contact, add its URI to the list box.
            foreach (Contact _Contact in group)
                // Test if contact already exists in subscription.
                if (newSubscription.Contacts.Contains(_Contact) == false)
                    // Add contact to contact subscription.

                    // Get contact information from the contact.
                    string uri = _Contact.Uri;


        // Callback method for the BeginAddGroup method.
        public void BeginAddGroupComplete(IAsyncResult res)

This article describes how to create and distribute a custom Lync control that is created by using Windows Presentation Foundation (WPF), and how to deploy the WPF applications that contain the control.

John Austin is a programmer/writer on the Microsoft Lync feature team. John has written several software development kits including Microsoft Office Communicator Web Access XML SDK, Microsoft Office Communicator 2007 Automation SDK, Microsoft Unified Communications Client Platform 1.0 SDK, and Microsoft Lync 2010 SDK.

John Clarkson is also a programmer/writer on the Microsoft Lync feature team.