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.
  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 Lync2010API solution.
  6. In Solution Explorer, right-click the LyncEventsLogger project and select Set as StartUp Project.
  7. Open the Window1.xaml.cs file of the LyncEventsLogger project.
  8. Select View >> Task List and select Comments from the menu.
  9. Start a remote desktop session with the secondary lab user Id.
  10. 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.
  2. 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();
  3. Navigate to TODO: 4.3.2.
  4. Add the following code after the TODO: 4.3.2 comment. This signs in to Lync 2010 if it is not currently signed in and specifies a callback method.

    C#

    _lyncClient.BeginSignIn(null, null, null, SignInCallback, "Local user signing in" as object);
  5. Navigate to TODO: 4.3.3.
  6. 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();
  7. Navigate to TODO: 4.3.4.
  8. Add the following code after the TODO: 4.3.4 comment. This gets references to Lync objects used to manage conversations, contacts and groups, and the signed-in user.

    _conversationManager = _lyncClient.ConversationManager; _contactManager = _lyncClient.ContactManager; _self = _lyncClient.Self;
  9. Sign out of Lync 2010.
  10. Go to Debug >> Start Without Debugging or press [Ctrl]+[F5] to start the application.
  11. Check that Lync 2010 signs in and the event logger tracks the sign in process.

  12. 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.
  2. 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);
  3. Press [Ctrl]+[F5] to start the application.
  4. Open Lync 2010, locate the secondary lab user’s contact card, and double-click to start an IM conversation.
  5. Observe the Conversation and Participant added events in the Event Log.

  6. 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);
  3. Navigate to TODO: 4.3.7.
  4. 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);
  5. Navigate to TODO: 4.3.8.
  6. 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);
  7. Press [Ctrl]+[F5] to start the application.
  8. Switch to the secondary lab user’s remote desktop session and change their status in Lync to Be Right Back.
  9. Return to the primary session and view the updated presence event for that contact in the Event Log.

  10. 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");
  3. Navigate to TODO: 4.3.10.
  4. 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();
  5. Navigate to TODO: 4.3.11.
  6. Add the following code after the TODO: 4.3.11 comment. This calls Self.BeginPublishContactInformation to 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");
  7. Press [Ctrl]+[F5] to start the application.
  8. Type “In a Conference” in the personal note text box and click the Publish button.
  9. Check for the event in the Event Log.

  10. Open Microsoft Lync 2010 and check that the new personal note is visible at the top of the window.

  11. In the Lync window, change the Availability to Busy.

  12. Check that the My Status area is updated and the event is logged.

  13. In the Update My Status menu of the application, change the status to Available and click the Update button.
  14. Check that the event is logged and the availability is updated in the Lync 2010 window.

  15. Close the application.