Exercise 2: Call Control and Presence Events

Task 1 – Open the Visual Studio Solution

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

  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\6\Source\Before.
  5. Open the IntroductionToUCMA solution.
  6. In Solution Explorer, right-click the CallControlandPresenceEvents project and select Set as Startup Project.
  7. In Solution Explorer, open the App.config file.
  8. Change the ApplicationId to the application id assigned to you, e.g. (urn:application:LabApp10600).
  9. Change the ApplicationName to the application name assigned to you, e.g. (LabApp10600)..
  10. Change the TrustedContactUri to the trusted contact uri assigned to you, e.g. (sip:LabContact10600@fabrikam.com).
  11. Change the PrimaryLabUserId and SecondaryLabUserId values to your primary and secondary lab accounts.
  12. Select View >> Task List and select Comments from the menu.

Task 2 – Register for an Incoming Call on the Application Endpoint

In this task, you will register the application endpoint to receive calls.

  1. In the Task List, navigate to TODO: 6.2.1.
  2. Add the following code after the TODO: 6.2.1 comment. Register for an incoming AudioVideoCall.

    C#

    _applicationEndpoint.RegisterForIncomingCall<AudioVideoCall>(AudioVideoCallReceived);
  3. Navigate to TODO: 6.2.2.
  4. Add the following code after the TODO: 6.2.2 comment. Get the incoming call and add a handler for call state changes.

    C#

    Console.WriteLine("\nIncoming audio call from {0}.", e.RemoteParticipant.Uri); _initialAVCall = e.Call; _initialAVCall.StateChanged += new EventHandler<CallStateChangedEventArgs>( AudioVideoCall_StateChanged);
  5. Navigate to TODO: 6.2.3.
  6. Add the following code after the TODO: 6.2.3 comment. Accept the incoming call.

    C#

    _initialAVCall.EndAccept(result);
  7. Go to Debug >> Start Without Debugging or use the shortcut key [CTRL]+[F5] to start the application.
  8. Check that the console displays messages indicating that the application endpoint has been registered for incoming calls.

  9. Switch to Microsoft Lync and search for your provisioned lab contact, e.g. LabContact10600@fabrikam.com.
  10. Click the Call button to place an audio call to the provisioned lab contact.
  11. Check the console for a message indicating that the application is receiving an incoming call and that the CallStateChanged event fires as the state changes from Incoming to Establishing and then from Establishing to Established.

  12. End the call by clicking the End Call button.
  13. Check that the console displays CallStateChanged events as the call’s state changes from Established to Terminating and then from Terminating to Terminated.

  14. Press Enter and close the console application.

Task 3 – Subscribe to Presence and Handle Presence Notification Events.

In this task, you will subscribe to presence notification events from the secondary lab user.

  1. Navigate to TODO: 6.2.4.
  2. Add the following code after the TODO: 6.2.4 comment. Subscribe to presence notifications for the secondary lab user.

    C#

    SubscribeToPresenceOfSecondaryLabUser();
  3. Navigate to TODO: 6.2.5.
  4. Add the following code after the TODO: 6.2.5 comment. Create a list of contacts to subscribe to presence notifications for.

    C#

    var subscriptionTargets = new List<RemotePresentitySubscriptionTarget>(); subscriptionTargets.Add(new RemotePresentitySubscriptionTarget(_secondaryLabUserId)); _remotePresenceView = new RemotePresenceView( _applicationEndpoint, new RemotePresenceViewSettings() { SubscriptionMode = RemotePresenceViewSubscriptionMode.Persistent }); // Wire up NotificationReceived before adding targets _remotePresenceView.PresenceNotificationReceived += new EventHandler<RemotePresentitiesNotificationEventArgs>(RemotePresenceView_NotificationReceived); // Immediately fires NotificationReceived with current presence of targets _remotePresenceView.StartSubscribingToPresentities(subscriptionTargets);
  5. Navigate to TODO: 6.2.6.
  6. Add the following code after the TODO: 6.2.6 comment. Process the presence notifications by determining the contact that each notification is referring to, and then checking if that contact is online. If the user is online, remove the notification event handler to avoid an infinite loop.

    C#

    foreach (var notification in e.Notifications) { // Verify that we are receiving a presence notification for the secondary lab user if (notification.Uri.Equals(_secondaryLabUserId, StringComparison.InvariantCultureIgnoreCase)) { if (IsUserAvailable(notification)) { // Unsubscribe from notifications _remotePresenceView.StartUnsubscribingToPresentities( new List<string>() { _secondaryLabUserId } ); _remotePresenceView.PresenceNotificationReceived -= new EventHandler<RemotePresentitiesNotificationEventArgs>(RemotePresenceView_NotificationReceived); Console.WriteLine("\n{0} is now available.", _secondaryLabUserId); // Present call transfer options to the user PresentCallTransferOptionsToUser(); } break; } }
  7. Navigate to TODO: 6.2.7.
  8. Add the following code after the TODO: 6.2.7 comment. Check if the secondary lab user is online and available.

    C#

    userIsOnline = notification.AggregatedPresenceState.Availability == PresenceAvailability.Online;
  9. Switch to the secondary lab user’s session and open Microsoft Lync.
  10. Set the secondary lab user’s status to Busy.
  11. Switch back to the primary lab user’s session.
  12. Go to Debug >> Start Without Debugging or use the shortcut key [CTRL]+[F5] to start the application.
  13. Place an audio call to the application contact.
  14. Check the console for a message indicating that it has subscribed to presence notification events for the secondary lab user.

  15. Go to the secondary lab user’s session and set the status to Available.
  16. Check the console for a message indicating the change in the secondary lab user’s presence.

  17. Press Enter and close the console application.

Task 4 – Perform an Unattended Call Transfer.

In this task, you will perform an unattended call transfer to the secondary lab user.

  1. Navigate to TODO: 6.2.8.
  2. Add the following code after the TODO: 6.2.8 comment. Present the user with the options for transferring a call.

    C#

    bool valid = false; var transferAction = new ConsoleKey(); ConsoleKeyInfo cki = new ConsoleKeyInfo(); while (!_selectedCallTransferAction) { Console.WriteLine("\nSelect a call transfer option to test:"); Console.WriteLine("1: Unattended call transfer"); Console.WriteLine("2: Attended call transfer with impersonation"); while (!valid) { cki = Console.ReadKey(true); if (cki.Key.Equals(ConsoleKey.D1) || cki.Key.Equals(ConsoleKey.D2)) { valid = true; _selectedCallTransferAction = valid; transferAction = cki.Key; } } } switch (transferAction) { case ConsoleKey.D1: TransferCallUnattended(); break; case ConsoleKey.D2: TransferCallAttendedWithImpersonation(); break; };
  3. Navigate to TODO: 6.2.9.
  4. Add the following code after the TODO: 6.2.9 comment. Begin the call transfer to the secondary lab user and provide a callback function.

    C#

    _initialAVCall.BeginTransfer( targetUri: _secondaryLabUserId, callTransferOptions: new CallTransferOptions(CallTransferType.Unattended), userCallback: result => { try { _initialAVCall.EndTransfer(result); } catch (RealTimeException e) { Console.WriteLine("Error transferring call: {0}", e.Message); } }, state: null);
  5. Go to the secondary lab user’s session and set the status to Busy.
  6. Go back to the primary lab user’s session.
  7. Press [CTRL]+[F5] to start the application.
  8. Place an audio call to the application contact.
  9. Go to the secondary lab user’s session and set the status to Available.
  10. Go to the console and press 1 to perform an unattended call transfer.
  11. Check the console for a message indicating that the original call has been transferred and then terminated.

  12. Switch to the secondary lab user’s session and answer the call.
  13. Switch back to the primary lab user’s session.
  14. Check the console for a message indicating that the call has been transferred to the secondary lab user.
  15. Note that with an unattended transfer, the original call is automatically transferred and terminated.

  16. End the call
  17. Press Enter and close the application.

Task 5 – Perform an Attended Call Transfer with Impersonation.

In this task, you will perform an attended call transfer to the secondary lab user.

  1. Navigate to TODO: 6.2.10.
  2. Add the following code after the TODO: 6.2.10 comment. Set the conversation to impersonate the primary lab user.

    C#

    conversation.Impersonate(uri: _primaryLabUserId, phoneUri: null, displayName: null);
  3. Navigate to TODO: 6.2.11.
  4. Add the following code after the TODO: 6.2.11 comment. Establish the call to the secondary lab user by providing an asynchronous callback.

    C#

    audioVideoCall.EndEstablish(result1);
  5. Navigate to TODO: 6.2.12.
  6. Add the following code after the TODO: 6.2.12 comment. Begin the transfer by providing the call object, the call options, and the callback method.

    C#

    _initialAVCall.BeginTransfer( callToReplace: audioVideoCall, callTransferOptions: new CallTransferOptions(CallTransferType.Attended), userCallback: result => { try { _initialAVCall.EndTransfer(result); } catch (OperationFailureException ofe) { Console.WriteLine("The recipient declined or did not answer the call: {0}", ofe.Message); } catch (RealTimeException e) { Console.WriteLine("Error transferring call: {0}", e.Message); } }, state: null);
  7. Go to the secondary lab user’s session and set the status to Busy.
  8. Go back to the primary lab user’s session.
  9. Press [CTRL]+[F5] to start the application.
  10. Place an audio call to the application contact.
  11. Go to the secondary lab user’s session and set the status to Available.
  12. Press 2 to perform an attended call transfer.

  13. Switch to the secondary lab user’s session and answer the call.
  14. Switch back to the primary lab user’s session.
  15. Check the console for messages indicating that the original call is only terminated after the transfer recipient picks up the transferred call.

  16. End the call.
  17. Press Enter and close the application.