使用Unified Communications Managed API獲取Lync在線會議的鏈接地址


最近在項目上遇到一個問題,需要能夠在程序中獲取Lync會議的鏈接地址。Lync是微軟出品的一套即時通信(IM)客戶端軟件,配合Microsoft Lync Server使用,其前身是Microsoft Office Communicator(MOC)。與Live Messenger相比,Lync更適合企業內部使用,因為它還具備一定的與企業級應用組件進行整合的功能。在Microsoft Office 2010/2013中,Lync已成為Office中的一個套件。

在企業內部使用Outlook和Lync的讀者一定知道,Outlook中有一個Lync的插件,當啟用了該插件后,即可在“日歷”視圖中通過單擊New Lync Meeting按鈕來安排一個帶有Lync在線會議鏈接地址的Appointment:

8-9-2013 4-51-42 PM

正如上圖所示,在這個Appointment窗口中,有個Join Lync Meeting的超級鏈接,鼠標移動到這個鏈接上,會提示出其所對應的超級鏈接地址,也就是Lync在線會議的鏈接地址。現在,我們就需要通過編寫C#程序的方式,來獲得這個Lync在線會議的鏈接地址。

SDK的選擇

經過上網搜索研究,與Outlook和Lync相關的SDK大致有以下幾種:

  1. Lync 2010/2013 SDK
  2. Exchange Web Services (EWS)
  3. Unified Communications Managed API (UCMA)
  4. Unified Communications Web API (UCWA)

Lync 2010/2013 SDK主要是通過Automation對象來實現Lync客戶端的操作,因此,使用Lync SDK時,Lync客戶端需要一直運行,否則無法獲得Automation對象。Lync SDK也支持Suppress UI的模式,這樣開發者就可以屏蔽Lync的用戶界面,而使用自己的軟件界面來使用Lync客戶端的功能,但無論是否是Suppress UI的模式,都需要Lync客戶端一直運行。

Exchange Web Services (EWS)是一套訪問Exchange Server的API,通過使用EWS,可以在程序中通過Exchange Server發送電子郵件、管理電子郵件、安排Outlook會議,以及管理自己的Outlook文件夾等等。

Unified Communications Managed API (UCMA) 是一套基於.NET的API,通過使用UCWA,可以獲得Microsoft Lync Server的增強狀態信息、即時消息、電話、視頻、音頻、會議等的訪問和控制功能。

Unified Communications Web API (UCWA) 是UCMA的Web版,通過Javascript、Json等技術向異構平台提供UCMA的功能。

終上所述,在我們的應用中,應該選擇Unified Communications Managed API (UCMA)來實現Lync在線會議鏈接地址的獲取。

代碼實現

首先需要安裝Unified Communications Web API,這可以到微軟的官方網站下載。下載安裝之后,即可開始編寫代碼。

創建一個控制台應用程序(Console Application),添加對Microsoft.Rtc.Collaboration.dll程序集的引用。之后,還需要會議組織者在企業中的電子郵件地址,以及Lync服務器的地址。前者容易獲得,讀者可以使用自己的郵件地址進行測試;而Lync服務器的地址,則可以在托盤中右鍵單擊Lync圖標,在彈出的菜單中選擇顯示Lync詳細信息的選項來獲得。

下面的代碼完整地展示了通過UCMA來獲取Lync在線會議的鏈接地址,具體內容請參考代碼中的注釋:

using System;
using System.Threading;
using Microsoft.Rtc.Collaboration;
using Microsoft.Rtc.Collaboration.ConferenceManagement;
using Microsoft.Rtc.Signaling;

namespace ConsoleApplication7
{
    class Program
    {
        static AutoResetEvent platformStartupCompleted = new AutoResetEvent(false);
        static AutoResetEvent endpointInitCompleted = new AutoResetEvent(false);
        static AutoResetEvent conferenceScheduleCompleted = new AutoResetEvent(false);

        static void Main(string[] args)
        {
            var ownerURI = "<在此填寫會議組織者URI,格式為:sip:郵箱地址>";
            var serverAddress = "<在此填寫Lync服務器地址>";
            var applicationName = "ConsoleApplication";
            // 通過ClientPlatformSettings來初始化一個CollaborationPlatform。
            // 注意,Transport Type應使用SipTransportType.Tls。
            ClientPlatformSettings clientPlatformSettings = new ClientPlatformSettings(applicationName, 
                SipTransportType.Tls);
            CollaborationPlatform platform = new CollaborationPlatform(clientPlatformSettings);
            // 啟動CollaborationPlatform實例
            platform.BeginStartup(platformEndStartup, platform);
            platformStartupCompleted.WaitOne();
            Console.WriteLine("Platform initialized...");

            // 通過UserEndpointSettings來初始化一個UserEndpoint。
            UserEndpointSettings userEndpointSettings = new UserEndpointSettings(ownerURI, serverAddress);
            userEndpointSettings.Credential = System.Net.CredentialCache.DefaultNetworkCredentials;
            UserEndpoint endpoint = new UserEndpoint(platform, userEndpointSettings);
            // 建立UserEndpoint的連接
            endpoint.BeginEstablish(endpointEndEstablish, endpoint);
            endpointInitCompleted.WaitOne();
            Console.WriteLine("Endpoint initialized...");

            // 設置會議的創建選項,詳細的設置選項請參考ConferenceScheduleInformation類的定義。
            ConferenceScheduleInformation conferenceScheduleInformation = new ConferenceScheduleInformation();
            conferenceScheduleInformation.AccessLevel = ConferenceAccessLevel.Invited;
            conferenceScheduleInformation.Description = "Conference Description";
            ConferenceParticipantInformation participantA_Information =
                new ConferenceParticipantInformation(ownerURI, ConferencingRole.Leader);
            conferenceScheduleInformation.Participants.Add(participantA_Information);
            conferenceScheduleInformation.LobbyBypass = LobbyBypass.EnabledForGatewayParticipants;
            conferenceScheduleInformation.AutomaticLeaderAssignment = AutomaticLeaderAssignment.SameEnterprise;
            ConferenceMcuInformation audioVideoMCU = new ConferenceMcuInformation(McuType.AudioVideo);
            conferenceScheduleInformation.Mcus.Add(audioVideoMCU);

            // 根據會議的創建選項創建新的Lync會議
            endpoint.ConferenceServices.BeginScheduleConference(conferenceScheduleInformation,
                conferenceEndScheduled, endpoint.ConferenceServices);
            
            conferenceScheduleCompleted.WaitOne();

            Console.WriteLine("Press anykey to exit.");
            Console.ReadLine();
        }

        static void conferenceEndScheduled(IAsyncResult ar)
        {
            try
            {
                ConferenceServices session = (ConferenceServices)ar.AsyncState;
                Conference conference = session.EndScheduleConference(ar);
                // 在控制台輸出Lync會議的鏈接地址
                Console.WriteLine(conference.WebUrl);
            }
            catch
            {
                throw;
            }
            finally
            {
                conferenceScheduleCompleted.Set();
            }
        }

        static void endpointEndEstablish(IAsyncResult ar)
        {
            try
            {
                UserEndpoint endpoint = ar.AsyncState as UserEndpoint;
                endpoint.EndEstablish(ar);
            }
            catch
            {
                throw;
            }
            finally
            {
                endpointInitCompleted.Set();
            }
        }

        static void platformEndStartup(IAsyncResult ar)
        {
            try
            {
                CollaborationPlatform collabPlatform = ar.AsyncState as CollaborationPlatform;
                collabPlatform.EndStartup(ar);
            }
            catch
            {
                throw;
            }
            finally
            {
                platformStartupCompleted.Set();
            }
        }
    }
}

執行效果

以下是本程序執行的結果,可以看到,程序已經可以在控制台輸出會議鏈接地址了:

image


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM