Hands-On Lab

Introduction to the Microsoft Lync 2010Managed API

Lab version:1.0

Last updated:10/16/2018

Contents

Overview

System Requirements

Exercise 1: Exploring the Capabilities of Automation

Task 1 – Beginning the Exercise

Task 2 – Starting an Instant Messaging Conversation

Task 3 – Starting an Audio Conversation

Task 4 – Sharing a Desktop

Task 5 – Transferring a File

Task 6 – Docking the Conversation Window

Exercise 2: Exploring the Lync 2010 API Object Model

Task 1 – Beginning the Exercise

Task 2 – Signing in to the Instance of Lync 2010

Task 3 – Setting up Event Handlers for the Lync Events

Task 4 – Setting up Event Handlers for Contacts and Groups

Task 5 – Publishing your Presence, Availability, and a Personal Note

Summary

Overview

Lab Time: 45 Minutes

Lab Folder:C:\%UC14TrainingKit%\Labs\4\Source\Before

The After folder contains the completed lab exercises.

Lab Overview: The Microsoft Lync 2010Managed API is a rich and full-featured client API that combines the automation capabilities of the Office Communicator Automation API with the flexibility and functionality of the UCC API. The Lync 2010 Managed API provides the Automation class to enable you to launch conversations, add contacts, and automate Lync within your applications.

The Lync 2010 Managed API also provides a rich object model that developers can use to build their own communications-enabled applications without the Lync 2010 UI.

In this lab solution, you will use the Lync 2010 Managed API to do the following:

  • Start an instant messaging conversation
  • Start an audio conversation
  • Start a desktop sharing conversation
  • Start a file transfer conversation
  • Dock and undock a conversation window
  • Prepare your application to sign in to Lync 2010
  • Set up event handlers for Lync events
  • Set up event handlers for Contact and Group events
  • Publish and retrieve presence items

System Requirements

You must have the following items to complete this lab:

  • Microsoft Visual Studio 2010
  • Microsoft Lync 2010
  • Microsoft Lync 2010 Controls SDK
  • Two accounts (referred to as the primary and secondary lab users in this document), provisioned for the Microsoft Lync Server 2010, that are able to successfully sign in to Lync 2010.

Exercise 1: Exploring the Capabilities of Automation

Task 1 – Beginning the Exercise

In this task, you will open the project and configure it to run with your accounts.

  1. Navigate to Start > All Programs > Microsoft Visual Studio 2010.
  2. Click on the Microsoft Visual Studio 2010 icon to start Visual Studio 2010.
  3. Select File > Open Project.
  4. Navigate to the folder C:\%UC14TrainingKit%\Labs\4\Source\Before.
  5. Open the Lync2010APIsolution.
  6. In Solution Explorer, right-click the AutomatingLyncproject and select Set as StartUp Project.
  7. In Solution Explorer, open theApp.config file of the AutomatingLyncproject.
  8. Change the PrimaryLabUserId and SecondaryLabUserId values to your primary and secondary lab accounts.
  9. Select View > Task List and select Comments from the menu.
  10. Start a remote desktop session with the secondary lab user Id.
  11. Return to the primary lab user’s session.

Task 2 – Starting an Instant Messaging Conversation

In this task, you will get the instance of Automation and start an instant messaging conversation with the secondary lab user.

  1. Double click TODO: 4.1.1.
  1. Add the following code after the TODO: 4.1.1 comment. This gets the Automation instance from the running instance of Lync.

C#

_automation = Microsoft.Lync.Model.LyncClient.GetAutomation();

  1. Double click TODO: 4.1.2.
  2. Add the following code after the TODO: 4.1.2 comment. Create a Dictionary of context data to add to the conversation, in this case specifying that the first instant message should be sent immediately to the recipient.

C#

var contextData = new Dictionary<AutomationModalitySettings, object>();

contextData.Add(AutomationModalitySettings.FirstInstantMessage, instantMessageText.Text);

contextData.Add(AutomationModalitySettings.SendFirstInstantMessageImmediately, true);

  1. Double click TODO: 4.1.3.
  2. Add the following code after the TODO: 4.1.3 comment. This starts an IM conversation by adding a participant, setting the context parameters, and specifying instant messaging mode.

C#

_automation.BeginStartConversation(

conversationModes: AutomationModalities.InstantMessage,

participantUris: participants,

contextData: contextData,

callback: StartConversationCallback,

state: _automation);

  1. Defining an optional callback method allows you to get a handle to the window.
  2. Go to Debug > Start Without Debugging or use the shortcut key [Ctrl]+[F5] to start the application.
  3. Enter “Hello” into the instant message textbox and click the Send IM button.

  1. Switch to the secondary lab user’s session and receive the message.
  2. Close the application.

Task 3 – Starting an Audio Conversation

In this task, you will start an audio conversation with the secondary lab user.

  1. Navigate to TODO: 4.1.4.
  1. Add the following code after the TODO: 4.1.4 comment. This starts an audio conversation by specifying audio mode.

C#

_automation.BeginStartConversation(

AutomationModalities.Audio,

participants,

null,

StartConversationCallback,

_automation);

  1. Press [Ctrl]+[F5] to start the application.
  2. Click the Start Audio button.
  3. Switch to the secondary lab user’s session and accept the call.
  1. Close the application.

Task 4 – Sharing a Desktop

In this task, you will share your desktop with the secondary lab user.

  1. Navigate to TODO: 4.1.5.
  1. Add the following code after the TODO: 4.1.5 comment. This shares a desktop by setting the context parameters and specifying the app sharing mode.

C#

var contextData = new Dictionary<AutomationModalitySettings, object>();

contextData.Add(AutomationModalitySettings.SharedDesktop, true);

_automation.BeginStartConversation(

AutomationModalities.ApplicationSharing,

participants,

contextData,

StartConversationCallback,

_automation);

  1. Press [Ctrl]+[F5] to start the application.
  2. Click the Share Desktop button.
  3. Switch to the secondary lab user’s session and join the desktop sharing conversation.
  1. Close the application.

Task 5 – Transferring a File

In this task, you will transfer a file to the secondary lab user.

  1. Navigate to TODO: 4.1.6.
  1. Add the following code after the TODO: 4.1.6 comment. This transfers a file by setting the context parameters to include the file transfer modality and the file path.

C#

var contextData = new Dictionary<AutomationModalitySettings, object>();

contextData.Add(AutomationModalitySettings.FilePathToTransfer, fileTransferPath.Text);

contextData.Add(AutomationModalitySettings.FileIsShared, true);

_automation.BeginStartConversation(

AutomationModalities.FileTransfer,

participants,

contextData,

StartConversationCallback,

_automation);

  1. Press [Ctrl]+[F5] to start the application.
  2. Click the Browse button to select a file to transfer.
  3. Select FileTransferSample.txt and click the Open button.

  1. Click the Transfer File button.
  2. Switch to the secondary lab user’s session and download the file.

  1. Close the application.

Task 6 – Docking the Conversation Window

In this task, you will dock and undock the conversation window.

  1. Navigate to TODO: 4.1.7.
  1. Add the following code after the TODO: 4.1.7 comment. This gets the handle to the conversation window.

C#

_conversationWindow = ((Automation)ar.AsyncState).EndStartConversation(ar);

Dispatcher.Invoke(new Action(() =>

{

if (_canDock)

{

dockConversationWindow.IsEnabled = true;

_conversationWindow.Move(_conversationWindow.Left, _conversationWindow.Top);

}

else

{

dockConversationWindow.IsEnabled = false;

}

}));

  1. Navigate to TODO: 4.1.8.
  2. Add the following code after the TODO: 4.1.8 comment adds event handlers for the conversation window events.

C#

_conversationWindow.NeedsSizeChange +=

new EventHandler<ConversationWindowNeedsSizeChangeEventArgs>(ConversationWindow_NeedsSizeChange);

_conversationWindow.NeedsAttention +=

new EventHandler<ConversationWindowNeedsAttentionEventArgs>(ConversationWindow_NeedsAttention);

  1. Navigate to TODO: 4.1.9.
  2. Add the following code after the TODO: 4.1.9 comment. This docks the window by calling its Dock method and passing the handle to the WindowsFormsHost control that will contain the docked window.

C#

_conversationWindow.Dock(windowsFormsHost.Handle);

  1. Navigate to TODO: 4.1.10.
  2. Add the following code after the TODO: 4.1.10 comment. This undocks the window by calling its Undock method.

C#

_conversationWindow.Undock();

  1. Press [Ctrl]+[F5] to start the application.
  2. Enter “Hello” into the instant message textbox and click the Send IM button.
  3. Click the Dock button.

  1. Verify that the IM conversation window’s chrome is removed and the conversation window is placed in the application window
  2. Drag the application window across the screen to see the IM window stay docked in the application.
  3. Click the Undock button.

  1. Verify that the IM window’s chrome returns and that the window is no longer bound to the application’s position.
  2. Close the application.

Exercise 2: Exploring the Lync 2010 API Object Model

Task 1 – Beginning the Exercise

In this task, you will open the project.

  1. Navigate to Start > All Programs > Microsoft Visual Studio 2010.
  1. Click on the Microsoft Visual Studio 2010 icon to start Visual Studio 2010.
  2. Select File > Open Project.
  3. Navigate to the folder C:\%UC14TrainingKit%\Labs\4\Source\Before.
  4. Open the Lync2010APIsolution.
  5. In Solution Explorer, right-click the LyncEventsLogger project and select Set as StartUp Project.
  6. Open the Window1.xaml.cs file of the LyncEventsLogger project.
  7. Select View > Task List and select Comments from the menu.
  8. Start a remote desktop session with the secondary lab user Id.
  9. Return to the primary lab user’s session.

Task 2 – Signing in to the Instance of Lync 2010

In this task, you will get a reference to the running instance of Lync 2010, sign in if necessary, and perform setup logic.

  1. Navigate to TODO: 4.3.1.
  1. Add the following code after the TODO: 4.3.1 comment. This gets the running instance of Lync 2010.

C#

_lyncClient = Microsoft.Lync.Model.LyncClient.GetClient();

  1. Navigate to TODO: 4.3.2.
  2. Add the following code after theTODO: 4.3.2 comment. This signs in to Lync 2010if it is not currently signed in andspecifies a callback method.

C#

_lyncClient.BeginSignIn(null, null, null, SignInCallback, "Local user signing in" as object);

  1. Navigate to TODO: 4.3.3.
  2. Add the following code after the TODO: 4.3.3 comment.This checks that sign in was successful and proceeds with initialization logic.

C#

LogEvent("SignInCallback", "Signing in to Lync");

InitializeClient();

  1. Navigate to TODO: 4.3.4.
  2. Add the following code after the TODO: 4.3.4 comment. This getsreferences to Lync objects used to manage conversations, contacts and groups, and the signed-in user.

C#

_conversationManager = _lyncClient.ConversationManager;

_contactManager = _lyncClient.ContactManager;

_self = _lyncClient.Self;

  1. Sign out of Lync 2010.
  2. Go to Debug > Start Without Debugging or press [Ctrl]+[F5] to start the application.
  3. Check that Lync 2010 signs in and the event logger tracks the sign in process.

  1. Close the application.

Task 3 – Setting up Event Handlers for the Lync Events

In this task, you will set up event handlers for the local user’s Lync account.

  1. Navigate to TODO: 4.3.5.
  1. Add the following code after the TODO: 4.3.5 comment. This sets up event handlers for events relating to the local account’s groups and conversations and presence using the Self.Contact reference.

C#

_conversationManager.ConversationAdded +=

new EventHandler<ConversationManagerEventArgs>(Conversations_ConversationAdded);

_conversationManager.ConversationRemoved +=

new EventHandler<ConversationManagerEventArgs>(Conversations_ConversationRemoved);

_self.Contact.ContactInformationChanged +=

new EventHandler<ContactInformationChangedEventArgs>(Self_ContactInformationChanged);

_contactManager.GroupAdded +=

new EventHandler<GroupCollectionChangedEventArgs>(ContactManager_GroupAdded);

_contactManager.GroupRemoved +=

new EventHandler<GroupCollectionChangedEventArgs>(ContactManager_GroupRemoved);

  1. Press [Ctrl]+[F5] to start the application.
  2. Open Lync 2010, locate the secondary lab user’s contact card, and double-click to start an IM conversation.
  3. Observe the Conversation and Participant added events in the Event Log.
  1. Close the application.

Task 4 – Setting up Event Handlers for Contacts and Groups

In this task, you will set up event handlers and subscriptions for groups and contacts.

  1. Navigate to TODO: 4.3.6.
  2. Add the following code after the TODO: 4.3.6 comment. This adds events handlers for individual groups.

C#

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

group.ContactRemoved += new EventHandler<GroupMemberChangedEventArgs>(Group_ContactRemoved);

  1. Navigate to TODO: 4.3.7.
  2. Add the following code after the TODO: 4.3.7 comment. This creates a subscription for Availability and Activity changes to presence items of your contacts.

C#

var subscription = _contactManager.CreateSubscription();

var contactInformationTypes =

new List<ContactInformationType>()

{

ContactInformationType.Availability,

ContactInformationType.Activity

};

subscription.Subscribe(ContactSubscriptionRefreshRate.Low, contactInformationTypes);

_contactSubscriptions.Add(group.Name, subscription);

  1. Navigate to TODO: 4.3.8.
  2. Add the following code after the TODO: 4.3.8 comment. This adds an event handler for each contact’s presence changed event.

C#

contact.ContactInformationChanged +=new EventHandler<ContactInformationChangedEventArgs>(

Contact_ContactInformationChanged);

  1. Press [Ctrl]+[F5] to start the application.
  2. Switch to the secondary lab user’s remote desktop session and change their status in Lync to Be Right Back.
  3. Return to the primary session and view the updated presence event forthat contact in the Event Log.
  1. Close the application.

Task 5 – Publishing your Presence, Availability, and a Personal Note

In this task, you will publish and retrieve your presence items.

  1. Navigate to TODO: 4.3.9.
  2. Add the following code after the TODO: 4.3.9 comment. This calls the Self.BeginPublishContactInformation method to publish a personal note for the local Lync account.

C#

var contactInformation = new List<KeyValuePair<PublishableContactInformationType, object>();

contactInformation.Add(new KeyValuePair<PublishableContactInformationType, object>(

PublishableContactInformationType.PersonalNote, personalNote.Text.Trim()));

_self.BeginPublishContactInformation(

contactInformation,

result => _self.EndPublishContactInformation(result),

"Publishing personal note");

  1. Navigate to TODO: 4.3.10.
  2. Add the following code after the TODO: 4.3.10 comment. This gets the Activity of the local Lync account (e.g. Available, Busy).

C#

myAvailability.Text = _self.Contact.GetContactInformation(ContactInformationType.Activity).ToString();

  1. Navigate to TODO: 4.3.11.
  2. Add the following code after the TODO: 4.3.11 comment. This callsSelf.BeginPublishContactInformationto change the availability of the local Lync account.

C#

var contactInformation = new List<KeyValuePair<PublishableContactInformationType, object>();

contactInformation.Add(new KeyValuePair<PublishableContactInformationType, object>(

PublishableContactInformationType.Availability,

Convert.ToInt32((status.SelectedValue as ComboBoxItem).Tag)));

_self.BeginPublishContactInformation(

contactInformation,

result => _self.EndPublishContactInformation(result),

"Updating my status");

  1. Press [Ctrl]+[F5] to start the application.
  2. Type “In a Conference” in the personal note text box and click the Publish button.
  3. Check for the event in the Event Log.
  1. Open Microsoft Lync 2010 and check that the new personal note is visible at the top of the window.
  1. In the Lync window, change the Availability to Busy.
  1. Check that the My Status area is updated and the event is logged.
  1. In the Update My Status menu of the application, change the status to Available and click the Update button.
  2. Check that the event is logged and the availability is updated in the Lync 2010 window.
  1. Close the application.

Summary

In this lab, you used the Microsoft Lync 2010Managed API to build simple applications that leverage Automation and the Lync 2010object model. You saw how easy it is to start conversations in different modalities (IM, Audio, Desktop Sharing, File Transfer) using Automation. You also learned how to configure your application to sign in to the running instance of Lync 2010, how to set up event handlers to listen for useful Lync events, and how to publish and retrieve your presence information.