0

I use the Lync SDK 2013 and try to check if a new conversation is incoming or outgoing. I don't want to check for audio/video calls only, I want to check in on each modality type.

private void Conversation_Added(object sender, ConversationManagerEventArgs e)
{
    Conversation conversation = e.Conversation;
    IDictionary<ModalityTypes, Modality> modalities = conversation.Modalities;
    bool conversationIsIncoming = modalities.Any(modality => modality.Value.State == ModalityState.Notified);
}

When the event gets triggered and it comes to the Any method I get this error

NullReferenceException object reference not set to an instance of an object. System.Collections.Generic.KeyValuePair.Value.get returned null.

So obviously I have to use a null check here but maybe the whole code may be wrong? How can I check if the conversation is incoming or outgoing?

  • 1
    [Please don't put tags in question titles](https://stackoverflow.com/help/tagging) – Liam Aug 17 '18 at 13:19

1 Answers1

0

Your idea is basically correct but when you check for the notified state is incorrect.

You need to hook the ModalityStateChanged event, and if you only want to know about audio/video "Calls" then you also only need to hook for conversations that have AudioVideo modality type.

e.g.

private void ConversationManager_ConversationAdded(object sender, ConversationManagerEventArgs e)
{
    if (e.Conversation.Modalities.TryGetValue(ModalityTypes.AudioVideo, out var avModality))
    {
        avModality.ModalityStateChanged += AvModalityOnStateChanged;
    }
}

private void AvModalityOnStateChanged(object sender, ModalityStateChangedEventArgs e)
{
    if (e.NewState == ModalityState.Notified)
    {
        bool conversationIsIncoming = true;
    }
}

Don't forget to unhook from the ModalityStateChanged when you don't need to know the state change any longer.

Shane Powell
  • 13,698
  • 2
  • 49
  • 61
  • I'm sorry, I don't only want to know about audio/video, I want to check it for all modality types –  Aug 20 '18 at 06:51
  • Then you need to hook all modalities, not just the avmodality. The code would basically be the same. – Shane Powell Aug 20 '18 at 07:13
  • Would you mind helping me again, I tried to setup your code but it seems I did not understand it https://hastebin.com/rulizivihi.cs –  Aug 21 '18 at 14:48
  • How do I setup the bool in my `Conversation_Added` event now =? –  Aug 21 '18 at 14:49
  • The point is that with the lync api you can’t tell until the modality state change callback if the call is incoming or not. Also, if u miss the event or don’t record then there is no way to tell. – Shane Powell Aug 21 '18 at 15:01
  • I haven’t played with IM modality states very much so u are assuming it follows the same states as a audio/video call. I would not assume that and log what events states u see and play around to see what up actually get out of lync... – Shane Powell Aug 21 '18 at 15:06