0

My ultimate aim is to transfer a file from one Lync client to another. I have following code.

First of all I have following 2 events registered 1.

((Modality)_conversation.Modalities[ModalityTypes.ContentSharing]).ModalityStateChanged += Modality_ModalityStateChanged;

2.

((ContentSharingModality)_conversation.Modalities[ModalityTypes.ContentSharing]).ContentAdded += _sharingModality_ContentAdded;

code for those event is

void _sharingModality_ContentAdded(object sender, ContentCollectionChangedEventArgs e)
        {
            MessageBox.Show("content added\n"+e.Item);
        }
        void Modality_ModalityStateChanged(object sender, ModalityStateChangedEventArgs e)
        {
            if (e.NewState == ModalityState.Connected)
            {
                textBox1.Text += "\nconnected";
                send_file();
            }
            if (e.NewState == ModalityState.Connecting)
            {
                textBox1.Text += "\nconnecting";
            }
        }

Then I have a method which creates a file in isolated storage named "abc.txt". Next there is a code which connects the content sharing modality.

private void button4_Click(object sender, RoutedEventArgs e)
    {
        if (_conversation.State == ConversationState.Active)
        {
                ((Modality)_conversation.Modalities[ModalityTypes.ContentSharing])
                .BeginConnect((ar) =>{((Modality)_conversation.Modalities[ModalityTypes.ContentSharing]).EndConnect(ar);              }
                , null);

        else { MessageBox.Show("conversation not active"); }
    }

After this there is 'send_file' method which actually upload the file. (this method id previously called when modality state changes to 'connected' but there (I think) conversation changes to multiparty and method returns false at 'canInvoke' statement. So Im calling it again and this time it succeeds. It is as below

void send_file()
        {
            if (((ContentSharingModality)_conversation.Modalities[ModalityTypes.ContentSharing]).State == ModalityState.Connected)
            {
                try
                {
                    if (((ContentSharingModality)_conversation.Modalities[ModalityTypes.ContentSharing]).CanInvoke(ModalityAction.CreateShareableNativeFileOnlyContent))
                    {
                        ContentSharingModality contentSharingModality = (ContentSharingModality)_conversation.Modalities[ModalityTypes.ContentSharing];
                        contentSharingModality.BeginCreateContentFromFile(ShareableContentType.NativeFile, "samplefile.txt", fileNameFromIsolatedStorage, true,
                            (ar) =>
                            {
                                ShareableContent sContent = contentSharingModality.EndCreateContentFromFile(ar);
                                //_NativeFileNameAndPath = string.Empty;
                                sContent.Upload();
                            }
                            , null);
                        MessageBox.Show("upload done");
                    }
                    else { MessageBox.Show("u cannot invoke"); }
                }
                catch (Exception e1) { MessageBox.Show(e1.Message); }
            }
            else { MessageBox.Show("modality inactive"); }
        }

Finally this is all I'm trying to do. The same code will lie on both sender & receiver machines. I'm new to lync development and very confused about what is going wrong. Please help. Thanks!

Mangesh
  • 5,491
  • 5
  • 48
  • 71
  • I have seen issues with the Lync reported it's Connected but it doesn't seem to be ready yet. Just throw a cheeky Thread.sleep(1000); before the canInvoke and see if that helps. – Paul Hodgson Apr 03 '14 at 05:44
  • I tried that but it didn't work out. What do you think about the code? Is it correct? Any suggestions? Is it necessary that client on other side also needs to be connected to content sharing modality to receive a file? I'm doubtful about about the path of file I'm providing. The file I'm uploading is in root directory of isolated store so I'm just providing its name as a parameter to uploading code. Is it the correct way? Thanks! – Mangesh Apr 03 '14 at 06:33
  • The code is correct & working fine when I provide local file path to BeginCreateContentFromFile method. My doubt was correct. Now the only problem is how to get that file path in isolated storage to make it work. (I don't mean physical path as said by Microsoft that 'you don't need to know actual path of isolated storage files on your disk'. – Mangesh Apr 03 '14 at 09:09

0 Answers0