Using UCMA 3.0 and Lync 2010 for Contextual Communication: Creating the UCMA Application (Part 3 of 6)

Summary:   This article is the third in a series of six articles that describe how to create a Microsoft Unified Communications Managed API (UCMA) 3.0 Core application that sets up a two-way contextual data channel with a Microsoft Lync 2010 application that uses Microsoft Silverlight. The Lync 2010 application runs in the Lync 2010 Conversation Window Extension.

This article describes the important details that appear in the Microsoft Unified Communications Managed API (UCMA) 3.0 application.

Applies to:   Microsoft Unified Communications Managed API (UCMA) 3.0 Core SDK | Microsoft Lync 2010 SDK

Published:   February 2011 | Provided by:   Mark Parker and John Clarkson, Microsoft | About the Authors

Contents

  • UCMA 3.0 Application Run-Time Tasks

  • Part 4

  • Additional Resources

Code Gallery  Download code

This article is the third in a six-part series of articles on using UCMA 3.0 and Lync 2010 for contextual communications.

UCMA 3.0 Application Run-Time Tasks

Three important UCMA 3.0 application tasks are associated with sending and receiving context data:

  • Establish the context channel by using the BeginEstablish method on the context channel instance.

  • Receive contextual data that identifies the item selected in the Lync 2010 application.

  • Send contextual data about the selected item to the Lync 2010 application.

The code appearing in the next example uses the BeginSendData method to send the string “Context channel is open.” to the Lync 2010 application.

Establish the Context Channel

Before the BeginEstablish method can be called to establish the context channel, its parameters must be assigned values. In the next example, a ConversationContextChannelEstablishOptions instance is created, and values are assigned to three of the ConversationContextChannelEstablishOptions properties. The value that is assigned to the guid parameter must be identical to the value that is used in the Lync 2010 application and in the registry key that the Lync 2010 application uses. For more information, see Register Contextual Conversation Packages in Lync 2010.

ConversationContextChannelEstablishOptions channelOptions = new ConversationContextChannelEstablishOptions();

channelOptions.ApplicationName = "Blue Yonder Airlines";
channelOptions.ContextualData = "Context channel is open."; // Normally used for initialization.
channelOptions.Toast = "Get ready for incoming contextual data.";

Guid guid = new Guid("3271E259-E508-4D39-B044-445855591E79");

// Establish a context channel to the remote endpoint.
_channel.BeginEstablish(guid, channelOptions, BeginEstablishCB, null);

The next example shows the callback method for the BeginEstablish method.

private void BeginEstablishCB(IAsyncResult result)
{
  if (_channel.State == ConversationContextChannelState.Establishing)
  {
    Console.WriteLine("\nChannel is in Establishing state.");
    _channel.EndEstablish(result);
  }
}

Receive Context Data

When the UCMA 3.0 application receives contextual data, the DataReceived event is raised. The handler for this event appears in the next example. This handler gets the body of the contextual data request, and then passes it to a helper method, SendDataToRemote.

// Event handler for the DataReceived event on ConversationContextChannel. 
void channel_DataReceived(object sender, ConversationContextChannelDataReceivedEventArgs e)
{
  Console.WriteLine("\nRequest data: {0}", e.RequestData.MessageType);
  Console.WriteLine("\nContent type: {0}", e.ContentDescription.ToString());


  // Assume that no more than 100 bytes are sent at a time.
  Byte[] body_byte = new Byte[100];
  body_byte = e.ContentDescription.GetBody();
  SendDataToRemote(body_byte);

  String body_UTF8 = null;
  body_UTF8 = Converter.ConvertByteArrayToString(body_byte, EncodingType.UTF8);

  Console.WriteLine("\nContent body: {0}", body_UTF8);
  _waitForChannelDataToBeReceived.Set();
}

Send Context Data

The SendDataToRemote method appearing in the next example converts the parameter that was passed in the call to this method to a String, and then compares this string with strings for the items in the Silverlight application. If the input string matches one of the known strings, this method calls the BeginSendData method to send more information about the selected item to the Lync 2010 application.

Note

The string to be sent using BeginSendData must be a Byte array.

private void SendDataToRemote(Byte[] data)
{
  // Each string to be sent consists of three fields, separated by commas or semicolons.
  String strCamera = "SLR X1000,$199.95,In stock";
  String strPhone = "ABC Smartfone,$149.50, In stock";
  String strGPS = "Gourmand XYZ,$210.00, On backorder";
  String strError = "Error,No item chosen,";
  String temp;

  // Convert data to String.
  String tempString = Converter.ConvertByteArrayToString(data, EncodingType.ASCII);

  if (tempString.Equals("Camera")) temp = strCamera;
  else if (tempString.Equals("Smart phone")) temp = strPhone;
  else if (tempString.Equals("GPS")) temp = strGPS;
  else temp = strError;

  // Convert temp to Byte[].
  Byte[] data_bytes = new byte[temp.Length];

  ASCIIEncoding data_ASCII = new ASCIIEncoding();
  data_bytes = data_ASCII.GetBytes(temp);

  ContentType contentType = new ContentType("text/plain; charset=us-ascii");

  _channel.BeginSendData(contentType, data_bytes, BeginSendDataCB, null);
  _waitForChannelDataToBeSent.WaitOne();
}

The next example shows the callback for the BeginSendData method that appears in the previous example.

private void BeginSendDataCB(IAsyncResult ar)
{
  _channel.EndSendData(ar);
  Console.WriteLine("\nEndSendData() called on channel.");
   _waitForChannelDataToBeSent.Set();
}

Part 4

Using UCMA 3.0 and Lync 2010 for Contextual Communication: Creating the Lync Application (Part 4 of 6)

Additional Resources

For more information, see the following resources:

About the Authors

Mark Parker and John Clarkson are programming writers with the Microsoft Lync product team.