Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

How to: Upload and download files to and from a Persistent Chat room

Published: July 16, 2012

Learn how to transfer files to and from a Persistent Chat room by using the Microsoft Lync Server 2013 Persistent Chat API.

To upload files to a Persistent Chat room, the chat room must be configured to support file upload. This feature is controlled at the category level by the system administrator. When a chat room supports file upload, the established chat room session has its IsFilePostAllowed property set to true. If this is false, uploading files to the underlying chat room will not succeed.

To upload a file to a joined chat room, call the BeginUploadFile(ChatRoomFileUploadJob, AsyncCallback, Object) and EndUploadFile(IAsyncResult) methods on a ChatRoomSession instance that has established a connection to the given chat room. The to-be-uploaded file is passed in to the call by using a ChatRoomFileUploadJob object, which also allows an application to monitor the progress of a file upload by raising the ProgressChanged events. To monitor the progress of a file upload job, an application must register for the event and implement an event handler.

The following code example shows how to upload a file to a chat room by using the Lync Server 2013 Persistent Chat API. The file is identified by the fileToUpload input parameter. The chat room is specified by the roomUri value. In the following example, the event handling of the ProgressChanged event is implemented by the job_ProgressChanged routine.


PersistentChatEndpoint chatEndpoint = ...;
ChatRoomSession ChatSession = new ChatRoomSession(chatEndpoint);

public void UploadFile(Uri roomUri, System.IO.FileInfo fileToUpload)
{
    if (!ChatSession.IsFilePostAllowed)
        return;
    JoinChatRoom(roomUri);

    ChatRoomFileUploadJob job = new ChatRoomFileUploadJob(fileToUpload);
    job.ProgressChanged += new EventHandler<ChatRoomFileTransferProgressEventArgs>(job_ProgressChanged);
    ChatSession.EndUploadFile(
        ChatSession.BeginUploadFile(job, null, null));
    LeaveChatRoom(roomUri);
}

void job_ProgressChanged(object sender, ChatRoomFileTransferProgressEventArgs e)
{
    Console.WriteLine("TotalBytes={0}, Percent transferred={1}%", e.TotalBytes, e.PercentTransferred);
}

One way to get a file is to use the OpenFileDialog control in the System.Windows.Forms namespace, as shown in the following code example.

static System.IO.FileInfo GetFileToOpen()
{
    System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
    if (System.Windows.Forms.DialogResult.OK == ofd.ShowDialog())
        return new System.IO.FileInfo(ofd.FileName);
    else
        return null;
}

To download a file from a joined chat room, call the BeginDownloadFile(ChatRoomFileDownloadJob, AsyncCallback, Object) and EndDownloadFile(IAsyncResult) methods on a ChatRoomSession instance that has established a connection to the given chat room. The to-be-downloaded file is passed in to the call by using a ChatRoomFileDownloadJob object, which also allows an application to monitor the progress of a file upload by raising ProgressChanged events. To monitor the progress of a file download job, an application must register for the event and implement an event handler.

The following code example shows how to download a file from a chat room by using the Lync Server 2013 Persistent Chat API. The to-be-downloaded file is the first one retrieved from the given chat room. It is extracted from a ChatMessage instance containing the file. The implementation is shown in the GetFileDownloadLink routine where the MessagePartFileDownloadLink object (downloadLink) is obtained by casting the MessagePart of a ChatMessage in the chat room. The destinationPath variable specifies the location where the downloaded file is saved. The chat room is specified by the roomUri value. The event handling of the ProgressChanged event uses the same job_ProgressChanged routine that is used to upload the file job.

public void DownloadFile(Uri roomUri, string destinationPath)
{
    if (!ChatSession.IsFilePostAllowed)
        return;

    JoinChatRoom(roomUri);
    var downloadLink = GetFileDownloadLink(roomUri);
    ChatRoomFileDownloadJob job = new ChatRoomFileDownloadJob(downloadLink, destinationPath);
    job.ProgressChanged += new EventHandler<ChatRoomFileTransferProgressEventArgs>(job_ProgressChanged);
    ChatSession.EndDownloadFile(
        ChatSession.BeginDownloadFile(job, null, null));
    LeaveChatRoom(roomUri);
}

MessagePartFileDownloadLink GetFileDownloadLink(Uri roomUri)
{
    ChatHistoryQueryOptions query = new ChatHistoryQueryOptions(null);            
    var chatHistory = ChatSession.EndQueryChatHistory(
        ChatSession.BeginQueryChatHistory(query, null, null));
    foreach (ChatMessage message in chatHistory.Messages)
    {
        foreach (MessagePart part in message.FormattedMessageParts)
        {
            if (part is MessagePartFileDownloadLink)
            {
                return part as MessagePartFileDownloadLink;
            }
        }
    }
    return null;
}

NoteNote

Files posted to a chat room will not be available to users of Microsoft Lync 2013.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.