Home > General > Logging Lync conversations

Logging Lync conversations

A feature missing in Lync is the ability to log chat conversations to a text file. There is an option to log conversations to the “Conversation History” folder in Outlook, but this option can be disabled by an administrator. I like being able view and search my conversation history. Since this option will be disabled soon, I need to find a way to continue logging my conversations. Fortunately for me, I can use the Lync SDK to my advantage.

Each time a chat or video session is started, they are encapsulated within a container. These containers are known as Conversations in Lync.

LyncClient client = LyncClient.GetClient();

client.ConversationManager.ConversationAdded += 
	(sender, eventArgs) => Console.WriteLine("Chat window opened.");

client.ConversationManager.ConversationRemoved += 
	(sender, eventArgs) => Console.WriteLine("Chat window closed.");

Conversations can contain multiple modes or “modalities”. In my case, I’m only interested in logging instant messages. On first try, my code looked similar to this:

client.ConversationManager.ConversationAdded += (sender, eventArgs) =>
	var instantMessageModality = 
		(InstantMessageModality) eventArgs.Conversation.Modalities[ModalityTypes.InstantMessage];

	instantMessageModality.InstantMessageReceived += (o, data) =>
		var mode = (InstantMessageModality) o;
		var name = (string) mode

		Console.WriteLine("Received a message from: " + name);
		Console.WriteLine("The message received is: " + data.Text);

I noticed the console was only printing messages from myself. When a message was received from another person, nothing happened. It turns out that I need to create an event handler for each person involved in the conversation. Since I was the original person who opened the chat window, the participant was set to myself.

Since multiple chat windows can be opened at the same time, we need to create a handler for each conversation window. Within each conversation window, we need to create a handler for each participant. For each participant, we need to create a handler to handle any messages we receive.

// For each conversation
client.ConversationManager.ConversationAdded += (cSender, cEventArgs) =>
	// For each participant
	cEventArgs.Conversation.ParticipantAdded += (pSender, pEventArgs) =>
		var modality = (InstantMessageModality) pEventArgs

		// Register for messages
		modality.InstantMessageReceived += (mSender, mEventArgs) =>

At this point, we have access to the person sending the message and the message text. We can append it to a file or log it to a database.

// Register for messages
modality.InstantMessageReceived += (mSender, mEventArgs) =>
	var instantMessageModality = (InstantMessageModality) mSender;
	var person = (string) instantMessageModality

	using (var conn = new SQLiteConnection(@"data source=D:\Lync\messages.db"))
	using (var cmd = conn.CreateCommand())

		cmd.CommandText = 
			@"insert into messages (conversationid, date, person, message) 
			values (@conversationid, @date, @person, @message)";
		cmd.Parameters.AddWithValue("@conversationId", conversationId);
		cmd.Parameters.AddWithValue("@date", DateTime.Now);
		cmd.Parameters.AddWithValue("@person", person);
		cmd.Parameters.AddWithValue("@message", mEventArgs.Text);

  1. September 29, 2014 at 4:28 am

    Good response in return of this query with genuine arguments
    and explaining everything regarding that.

  2. September 29, 2014 at 2:41 pm

    I love your blog.. very nice colors & theme. Did you create this website yourself or did you hire someone to do it for you?
    Plz answer back as I’m looking to create my own blog and would like to know where u got this from.

  3. September 30, 2014 at 10:08 am

    Good post. I learn something totally new and challenging on blogs I stumbleupon every day.
    It’s always helpful to read through articles from other authors and use a little something from other

  4. October 1, 2014 at 1:58 pm

    This blog was… how do I say it? Relevant!! Finally I’ve found something that helped me.


  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: