Send and Update Context in a UCMA Conversation with Lync 2010 SDK
Use Microsoft Lync 2010 SDK and Microsoft Lync 2010 to participate in a Microsoft Unified Communications Managed API (UCMA) 3.0 conversation. The UCMA 3.0 application hosts the conversation and starts the context channel, and the Lync 2010 client responds to the INVITE event from UCMA 3.0 to join the conversation.
On the UCMA 3.0 computer, create and establish a UserEndPoint endpoint, and then add a remote endpoint to represent the Lync 2010 conversation endpoint.
On the Lync 2010 client, use the Conversation.GetApplicationData method to receive data, and the Conversation.BeginSendContextData method to send data.
Creating the Conversation on the UCMA 3.0 Computer
The following steps describe how to create and manage a conversation on the UCMA 3.0 computer.
Create a CollaborationPlatform instance.
Create and establish an endpoint by using a previously created CollaborationPlatform instance and endpoint settings.
Create a Conversation instance.
Create a call, typically by using an instance of the InstantMessagingCall or AudioVideoCall class.
Place a call to the remote endpoint by using the BeginEstablish method on the call. Specify the SIP URI. An INVITE is sent to the Lync SDK application, and the first toast starts to run on the Lync 2010 computer.
Create a new ConversationContextChannel instance, passing the conversation and remote endpoint as parameters.
Create a ConversationContextChannelEstablishOptions instance, and set the appropriate property values. The properties in the next example identify the application.
channelOptions.ApplicationInstallerPath = "https://www.msn.com"; channelOptions.ApplicationName = "Blue Yonder Airlines"; channelOptions.ContextualData = "Contextual data from UCMA"; channelOptions.RemoteConversationId = "1234"; channelOptions.SimpleLink = ""; channelOptions.Toast = "Some contextual data is coming in this call.";
Establish the ConversationContextChannel instance by calling the BeginEstablish method. Pass the GUID in the ConversationContextChannelEstablishOptions instance. Another INVITE is sent and the second toast starts to run on the Lync 2010 computer.
When the ConversationContextChannel instance is in the Established state, call BeginSendData to send contextual data to the remote endpoint.
When the remote endpoint responds through the context channel, the DataReceived event on the UCMA 3.0 side is raised. This event contains contextual data in the event arguments.
The channel is now open and you can exchange data. When no more data will be sent, call BeginTerminate on the channel.
Sending and Receiving Context from the Lync 2010 Computer
Lync 2010 gets the application invite from UCMA 3.0, sets up the conversation window and opens the Conversation Window Extension with a Microsoft Silverlight application that contains information about the caller. Once the SIP conversation is established the conversation window appears, and then UCMA 3.0 and Lync 2010 can exchange data over the channel both ways.
The following example shows how to use Conversation object methods to send and receive context as soon as the conversation session starts. For more information, see Send and Update Context in an Existing Conversation with Lync 2010 SDK.
Receiving Content
On the Lync 2010 client, use the Conversation.GetApplicationData method to receive data. See the following example, especially the GetAppData_Click event.
Sending Content
Use the Conversation.BeginSendContextData method to send data. See the following example, especially the SendData_Click event.
Example
Use Conversation object methods appearing in the next example to send and receive contextual data in a conversation with a UCMA 3.0 application.
private void Logger(string text)
{
LoggerTextBox.Text += text + "\n";
}
private void Initialize()
{
try
{
_conversation = (Conversation)Microsoft.Lync.Model.LyncClient.GetHostingConversation();
_conversation.InitialContextReceived += OnInitialContextReceived;
_conversation.InitialContextSent += OnInitialContextSent;
_conversation.ContextDataReceived += OnContextDataReceived;
_conversation.ContextDataSent += OnContextDataSent;
}
catch (Exception ex)
{
Logger("Exception: " + ex);
}
}
// Occurs when an application receives context data after the initial receipt of data.
// Useful to avoid having to poll to see if more data arrived.
public void OnContextDataReceived(object sender, ContextEventArgs args)
{
try
{
Logger("OnContextDataReceived: AppId = " + args.ApplicationId + " DataType = " + args.ContextDataType + " Data = " + args.ContextData);
}
catch (Exception ex)
{
Logger("Exception: " + ex);
}
}
// Occurs when an application sends context data after the initial transmission of data.
public void OnContextDataSent(object sender, ContextEventArgs args)
{
try
{
Logger("OnContextDataSent: AppId = " + args.ApplicationId + " DataType = " + args.ContextDataType + " Data ++oncontextSent++ = " + args.ContextData);
}
catch (Exception ex)
{
Logger("Exception: " + ex);
}
}
// Occurs when an application initially sends context data.
public void OnInitialContextSent(object sender, InitialContextEventArgs args)
{
try
{
Logger("OnInitialContextSent: AppId = " + args.ApplicationId + " AppData ++oncontextSent++ = " + args.ApplicationData);
}
catch (Exception ex)
{
Logger("Exception: " + ex);
}
}
// Occurs when an application initially receives context data.
public void OnInitialContextReceived(object sender, InitialContextEventArgs args)
{
try
{
Logger("OnInitialContextReceived: AppId = " + args.ApplicationId + " AppData = " + args.ApplicationData);
}
catch (Exception ex)
{
Logger("Exception: " + ex);
}
}
private void GetAppData_Click(object sender, RoutedEventArgs e)
{
try
{
// Receive data from the UCMA application.
string appData = _conversation.GetApplicationData(AppId);
Logger("AppData = " + appData);
}
catch (Exception ex)
{
Logger("Exception: " + ex);
}
}
private void SendData_Click(object sender, RoutedEventArgs e)
{
try
{
Logger("Sending additional context: DataType = " + DataTypeBox.Text + " Data = " + AppDataBox.Text);
// Send data to the UCMA application.
// AppId parameter is the application GUID.
// The next two parameters are text box entries from the application UI.
// The fourth parameter is the name of the callback method.
_conversation.BeginSendContextData(AppId, DataTypeBox.Text, AppDataBox.Text, SendAdditionalDataCallBack, null);
}
catch (Exception ex)
{
Logger("Exception: " + ex);
}
}
private void SendAdditionalDataCallBack(IAsyncResult asyncResult)
{
if (asyncResult.IsCompleted)
{
_conversation.EndSendContextData(asyncResult);
Logger("Additional Context Sent successfully.");
}
else
{
Logger("Additional Context Sent Failed : " + asyncResult.AsyncState);
}
}
See Also
Other Resources
Lync Extensibility API Contextual Conversations (Lync 2010 SDK)