使用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

posted @ 2013-08-11 12:55  dax.net  阅读(4231)  评论(9编辑  收藏  举报