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

Summary:   Part 2 contains a code listing for the application that is 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 Microsoft Silverlight page .xaml file. This code creates three buttons that start Click event handlers in the code behind, and a list box to display URIs.

<navigation:Page x:Class="SilverlightClassLibrary1.LyncCustomControl" 
           d:DesignWidth="640" d:DesignHeight="480"
           Title="LyncCustomControl Page">
    <Grid x:Name="LayoutRoot">
        <Button Click="button1_Click" Content="Create Group" Height="23" HorizontalAlignment="Left" Margin="24,12,0,0" Name="button1" VerticalAlignment="Top" Width="127" />
        <Button Click="button2_Click" Content="List Contacts" Height="23" HorizontalAlignment="Left" Margin="24,68,0,0" Name="button2" VerticalAlignment="Top" Width="127" />
        <Button Click="button3_Click" Content="Send Invites" Height="23" HorizontalAlignment="Left" Margin="24,119,0,0" Name="button3" VerticalAlignment="Top" Width="127" />
        <ListBox Height="100" HorizontalAlignment="Left" Margin="185,42,0,0" Name="listBox1" VerticalAlignment="Top" Width="175" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="24,193,0,0" Name="textBox1" VerticalAlignment="Top" Width="262" />

The following example shows the content of the Silverlight page code-behind file. This code creates a new user group in the Microsoft 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 Silverlight Control: How to (Part 1 of 2).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

using Microsoft.Lync.Model;
using Microsoft.Lync.Model.Conversation;
using Microsoft.Lync.Model.Extensibility;
using Microsoft.Lync.Model.Group;
using System.ComponentModel;

namespace SilverlightClassLibrary1
    public partial class LyncCustomControl : Page
        ContactManager contactManager;
        LyncClient client;

        public LyncCustomControl()
            if ((bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue))

                client = LyncClient.GetClient();
                contactManager = client.ContactManager;
                listBox1.SelectionMode = SelectionMode.Multiple;
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }


        // Adds custom group contacts to the listbox control.
        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.
            foreach (Contact _Contact in group)
                // Test if contact already exists in subscription.
                if (newSubscription.Contacts.Contains(_Contact) == false)
                    // Add contact to contact subscription.

                    // Add contact URI to the list box.
                    string uri = _Contact.Uri;

        #region event handlers
        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)

        // Button 1 click event handler.
        // Create a custom group called 'sample'.
        private void button1_Click(object sender, RoutedEventArgs e)
            MessageBox.Show("Adding custom group..");
                string groupName = "Sample";
                contactManager.GroupAdded += new EventHandler<GroupCollectionChangedEventArgs>(contactManager_GroupAdded);
                AsyncCallback callback = new AsyncCallback(BeginAddGroupComplete);
                contactManager.BeginAddGroup(groupName, BeginAddGroupComplete, null);
            catch (Exception ex)


        // Button 2 click event handler.
        // Find the contacts in the custom group, calls the GetGroupContacts method.
        private void button2_Click(object sender, RoutedEventArgs e)
            MessageBox.Show("Listing contacts...");
            foreach (Group _Group in contactManager.Groups)
                if (_Group.Type == GroupType.CustomGroup)

        // Button 3 click event handler.
        // Sends IM to selected contacts.
        private void button3_Click(object sender, RoutedEventArgs e)
            MessageBox.Show("Sending IM...");
            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 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
                , EndStartConversation
                , null);


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

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

        #endregion event handlers

        #region callback methods
        // Callback method for the ContactManager.BeginAddGroup method.
        public void BeginAddGroupComplete(IAsyncResult res)

        // Callback method for the Automation.BeginStartConversation.
        public void EndStartConversation(IAsyncResult res)
            Automation automation = LyncClient.GetAutomation();

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

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

        #endregion callback methods

This article describes how to create and distribute a custom Lync Silverlight control, and how to deploy the Silverlight application that contains 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.