经过长时间的尝试,我能够根据我的需要实现功能。在这里,我将解释要遵循的步骤。
我使用“Communicator API”和“NameCtrl”来实现这一点
1)参考这个链接http://msdn.microsoft.com/en-us/library/bb787231%28v=office.12%29.aspx
为什么我要提到这个,因为要使用 Microsoft Office Communicator 自动化 API 开发应用程序,必须满足以下要求:
Microsoft Office Communicator 2007 安装在您的开发计算机上。
Microsoft Office Communicator 2007 SDK 安装在开发机器上。SDK 可从 MSDN 下载。
2) 在服务器上安装 Lync2010 并登录。
3) 从您的 Web 应用程序中添加对 CommunicatorAPI.dll 和 CommunicatorPrivate.dll 的引用
我在这里使用从“ http://www.microsoft.com/en-us/download/details.aspx?id=10503 ”下载的通信器图像。这是一个msi文件。下载并执行。在演示中,您可以复制这些图像并添加到您的应用程序中
这是完整的解决方案。
HTML
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MARSWebCommunicator._Default" %>
<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
<section class="featured">
<div class="content-wrapper">
</div>
</section>
<script src="Scripts/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript">
var sipUri = "chandan.kumarpanda@yourdomain.com";
var nameCtrl = new ActiveXObject('Name.NameCtrl.1');
$(document).ready(function () {
sipUri = $("#<%=HiddenField1.ClientID %>").val();
if (sipUri != "") {
if (nameCtrl.PresenceEnabled) {
nameCtrl.OnStatusChange = onStatusChange;
nameCtrl.GetStatus(sipUri, "1");
}
}
});
function onStatusChange(name, status, id) {
// This function is fired when the contacts presence status changes.
// In a real world solution, you would want to update an image to reflect the users presence
//alert(name + ", " + status + ", " + id);
}
function ShowOOUI() {
nameCtrl.ShowOOUI(sipUri, 0, 15, 15);
}
function HideOOUI() {
nameCtrl.HideOOUI();
}
</script>
</asp:Content>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<div>
<asp:HiddenField ID="HiddenField1" runat="server" />
</div>
<div id="dvContactdetails">
<table border="1">
<tr>
<td>Email Id</td>
<td>Name</td>
<td>Status</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="drpEmails" AutoPostBack="true" runat="server" OnSelectedIndexChanged="drpEmails_SelectedIndexChanged"></asp:DropDownList></td>
<td>
<asp:Image ID="Image1" onmouseover="ShowOOUI()" onmouseout="HideOOUI()" ImageUrl="presence_images/presence_16-unknown.bmp" runat="server" />
<asp:Label ID="lblName" runat="server" Text="Contact Name"></asp:Label>
</td>
<td>
<asp:Label ID="lblStatus" runat="server" Text="Contact Status"></asp:Label></td>
</tr>
</table>
</div>
</asp:Content>
代码隐藏
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunicatorAPI;
namespace MARSWebCommunicator
{
public partial class _Default : Page
{
CommunicatorAPI.Messenger communicator = null;
List<CustomContacts> lstContactDetails;
protected void Page_Load(object sender, EventArgs e)
{
lstContactDetails = new List<CustomContacts>();
communicator = new CommunicatorAPI.Messenger();
string mymailid = "Chandan.kumarpanda@yourdomain.com";
var contact = GetContact(mymailid);
int s = (int) contact.Status;
lblStatus.Text = GetStatus(s);
if (!Page.IsPostBack)
{
drpEmails.Items.Add(mymailid);
lblName.Text = contact.SigninName.ToString();
HiddenField1.Value = contact.SigninName.ToString();
lstContactDetails = GetAllEmailsWithFiendlyName();
foreach (CustomContacts aContact in lstContactDetails)
{
drpEmails.Items.Add(aContact.ContactEmailId);
}
}
}
protected void drpEmails_SelectedIndexChanged(object sender, EventArgs e)
{
string currentmailid = drpEmails.SelectedItem.Text;
var contact = GetContact(currentmailid);
lblName.Text = contact.FriendlyName.ToString();
int s = (int)contact.Status;
lblStatus.Text = GetStatus(s);
if (HiddenField1.Value != "")
{
HiddenField1.Value = "";
HiddenField1.Value = contact.SigninName.ToString();
}
}
protected string GetStatus(int s)
{
string status = string.Empty;
string src = string.Empty;
int tempstatusno = s;
switch (s)
{
case 0 :
status = "UNKNOWN";
src = "presence_images/presence_16-unknown.bmp";
break;
case 1:
status = "OFFLINE";
src = "presence_images/presence_16-off.bmp";
break;
case 2:
status = "ONLINE";
src = "presence_images/presence_16-online.bmp";
break;
case 6:
status = "INVISIBLE";
src = "presence_images/presence_16-unknown.bmp";
break;
case 10:
status = "BUSY";
src = "presence_images/presence_16-busy.bmp";
break;
case 14:
status = "BE_RIGHT_BACK";
src = "presence_images/presence_16-idle-busy.bmp";
break;
case 18:
status = "IDLE";
src = "presence_images/presence_16-idle-online.bmp";
break;
case 34:
status = "AWAY";
src = "presence_images/presence_16-away.bmp";
break;
case 50:
status = "ON_THE_PHONE";
break;
case 66:
status = "OUT_TO_LUNCH";
break;
case 82:
status = "IN_A_MEETING";
break;
case 98:
status = "OUT_OF_OFFICE";
break;
case 114:
status = "DO_NOT_DISTURB";
src = "presence_images/presence_16-dnd.bmp";
break;
case 130:
status = "IN_A_CONFERENCE";
break;
case 146:
status = "ALLOW_URGENT_INTERRUPTIONS";
break;
case 162:
status = "MAY_BE_AVAILABLE";
break;
case 178:
status = "CUSTOM";
break;
default:
status = "OFFLINE";
src = "presence_images/presence_16-unknown.bmp";
Image1.ImageUrl = src;
break;
}
Image1.ImageUrl = src;
return status;
}
public IMessengerContact GetContact(string signinName)
{
return communicator.GetContact(signinName, communicator.MyServiceId) as IMessengerContact;
}
public List<CustomContacts> GetAllEmailsWithFiendlyName()
{
List<CustomContacts> lstContacts = new List<CustomContacts>();
IMessengerContacts messengerContacts = (IMessengerContacts)communicator.MyContacts;
foreach (IMessengerContact acontact in messengerContacts)
{
CustomContacts aContact = new CustomContacts();
aContact.ContactName = acontact.FriendlyName.ToString();
aContact.ContactEmailId = acontact.SigninName.ToString();
lstContacts.Add(aContact);
}
return lstContacts;
}
}
public class CustomContacts
{
public string ContactEmailId { get; set; }
public string ContactName { get; set; }
public string ContactStatus { get; set; }
}
}