http://www.felixwoo.com/wp-content/uploads/attachments/200606/21_170515_login.jpg
  这两天为SharePoint开发了一个获取Exchange未读邮件数的WebPart。存取Exchange信息可以有很多种方法,象MAPI、CDO和WebDAV。我采用的就是WebDAV方法。WebDAV实际上是基于HTTP的一种协议,返回的标准XML数据。优点基于国际标准、方便远程应用、调试方便而且适用于任何程序语言,那缺点主要就是要自己处理返回的XML结果。
  我的开发平台是.NET,主要的思路是使用System.Net.HttpWebRequest类来获取远程的WebDAV,然后分析返回的XML,提取出其中的“未读邮件”节点的数字。其中很重要的一点便是身份验证。因为Exchange的WebDAV是根据不同的用户返回不同邮箱的数据,当然就是非匿名的。因此,需要把客户端当前登录的Credential(用户身份凭据)传递到Exchange服务器上。然而我的Exchange服务器和SharePoint网站服务器是两台不同的服务器,如何才能把客户端的身份凭据通过Web服务器传递到Exchange服务器上呢?这个问题浪费了我不少时间才解决,我将再下一篇文章《使用Kerberos解决身份凭据传递问题》中详细介绍解决方法。
  参考了Exchange SDK中的Sample,获取Exchange信息的主要代码如下:

private int GetUnReadMailCount()
{
string url=“http://mail.felixwoo.com/exchange/”; //指定Exchange服务器地址
System.Net.HttpWebRequest Request;
System.Net.WebResponse Response;
System.Net.CredentialCache MyCredentialCache;
string strUserName = “wuf”; //指定登录的用户名
string strRootURI = url+strUserName ; //得到要访问邮箱的WebDAV地址
string strPassword = “123456”; //指定该用户的密码
string strDomain = “felixwoo.com”; //指定域名
string strQuery ="";
byte[] bytes = null;
System.IO.Stream RequestStream = null;
System.IO.Stream ResponseStream = null;
XmlDocument ResponseXmlDoc = null;
XmlNodeList HrefNodes= null;
XmlNodeList SizeNodes= null;
int count=0;
try
{
  // 用SQL查询WebDAV返回结果中的unreadcount节点.
  strQuery = "<?xml version=\"1.0\"?><D:searchrequest xmlns:D = \"DAV:\" >"
   + "<D:sql>SELECT \"DAV:displayname\",\"urn:schemas:httpmail:unreadcount\" FROM \"" + strRootURI + "\""
   + "</D:sql></D:searchrequest>";

  // 创建新的CredentialCache对象,构建身份凭据
  MyCredentialCache = new System.Net.CredentialCache();
  MyCredentialCache.Add( new System.Uri(strRootURI),
   "NTLM",
   new System.Net.NetworkCredential(strUserName, strPassword, strDomain)
   );

  // Create the HttpWebRequest object.
  Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strRootURI);

  // 指定HttpWebRequest的身份凭据,此处为关键所在。如果使用之前
  // 创建的MyCredentialCache,则这个身份凭据是可以从Web服务器传递
  // 到Exchange服务器的,但是这样带来的问题也很明显,就是不能够自
  // 动获取当前登录到域的用户的身份。即便已经成功登录到域,那也只
  // 能通过form再次输入用户名密码。因此,我在这里用的是
  // Request.Credentials = CredentialCache.DefaultCredentials,
  // 这样便可以获得当前用户的凭据,但是这样带来的问题便是上面提到的
  // 身份凭据无法传递的问题,解决方法请关注下篇文章。
  Request.Credentials = MyCredentialCache;

  // 指定WebDAV的SEARCH方法
  Request.Method = "SEARCH";

  // Encode the body using UTF-8.
  bytes = Encoding.UTF8.GetBytes((string)strQuery);

  // Set the content header length. This must be
  // done before writing data to the request stream.
  Request.ContentLength = bytes.Length;

  // Get a reference to the request stream.
  RequestStream = Request.GetRequestStream();

  // Write the SQL query to the request stream.
  RequestStream.Write(bytes, 0, bytes.Length);

  // Close the Stream object to release the connection
  // for further use.
  RequestStream.Close();

  // Set the content type header.
  Request.ContentType = "text/xml";

  // Send the SEARCH method request and get the
  // response from the server.
  Response = (HttpWebResponse)Request.GetResponse();

  // Get the XML response stream.
  ResponseStream = Response.GetResponseStream();

  // 创建XmlDocument对象,并获取收件箱的unreadcount节点的值
  ResponseXmlDoc = new XmlDocument();
  ResponseXmlDoc.Load(ResponseStream);
  HrefNodes = ResponseXmlDoc.GetElementsByTagName("a:displayname");
  SizeNodes = ResponseXmlDoc.GetElementsByTagName("d:unreadcount");
  for(int i=0;i<HrefNodes.Count;i++)
  {
   if(HrefNodes[i].InnerText=="收件箱")
    count=int.Parse(SizeNodes[i].InnerText);
  }
  ResponseStream.Close();
  Response.Close();
}
catch(Exception)
{
  // Catch any exceptions. Any error codes from the SEARCH
  // method request on the server will be caught here, also.
  return -1;
}
return count;
}

单位的门户网站构建于SharePoint,即时通讯平台是Live Communication Server(LCS)。由于LCS并不自带查询聊天记录的功能,所以为SharePoint开发了一个查询LCS聊天记录的WebPart。在此记录一下创建的过程。

http://www.felixwoo.com/wp-content/uploads/attachments/200606/15_151026_lcshistory.jpg

一、创建前的准备
1、服务器端安装Sharepoint Portal Server
2、开发客户端安装Visual Studio .NET
3、安装WebPart模版Sharepoint Web Part Library Template for Visual Studio .NET
下载地址:http://www.microsoft.com/downloads/details.aspx?FamilyId=14D5D92F-C3A6-407C-AAD7-B8C41A4991BE&displaylang=en。在安装这个模版的时候需要指定Microsoft.SharePoint.dll的位置,需要从服务器上把该文件拷贝到开发端的本地。

二、开发步骤
1、首先创建查询LCS记录User Control。LCS的聊天记录保存在默认名为LcsLog的数据库中,其中会用到messages和users两张表,在messages中保存了聊天记录的内容,但是其中的发送和接受人都是以id形式表现,需要用此id再到users中查到相应的Email,最后再利用DirectoryService从Active Directory中获取对应的用户名称。最后保存为History.ascx。

2、创建 Web Control Library 项目,相关的引用和namespace等都会自动创建好。

3、在项目中通过“添加现有项…”菜单,将第一步中建立的UserControl的三个文件全部加入到此项目中。打开“History.ascx.cs”文件,将此文件第一行指定的namespace改成和此项目一致的“LCS”,打开“History.ascx”文件,将第一行的“<%@ Control>”标签中的“CodeBehind”属性删除,“Inherits”属性的值改为“LCS.History”。

4、转到步骤2创建的“WebPart1.cs”文件,定义一个用来保存UserControl的对象:private System.Web.UI.Control _innerControl; 然后重载CreateChildControls()方法

protected override void CreateChildControls()
{
this._innerControl = this.Page.LoadControl("/bin/History.ascx");
this.Controls.Add(this._innerControl);
}

再重载RenderWebPart()方法,输出载入的User Control:

protected override void RenderWebPart(HtmlTextWriter output)
{
this.EnsureChildControls();
this._innerControl.RenderControl(output);
}

5、最后编辑“WebPart1.dwp”,配置WebPart。编辑SPS虚拟站点上的“web.config”,添加“<SafeControl>”标签以信任我们的WebPart。在VS.NET中编译,生成最终的“LCS.dll”。将此(.dll)和History.ascx拷贝到SPS虚拟站点跟目录的“bin”目录下。在SPS站点页面中导入WebPart1.dwp。这样查询LCS聊天记录的WebPart就可以使用了。

本文只是大体讲解了一下整个过程,具体关于开发Webpart的教程可以下载Kaneboy写的PPT教程:
http://www.gotdotnet.com/workspaces/releases/releasedownloadhandler/download.aspx/7741ec2a-e7ab-41da-baef-a8efd2658895/85aefa87-fb76-4b32-ab12-31a4e647878a/download.zip

京ICP备05053527号
经过26次查询历时0.404秒终于生成了此页面
Powered by WordPress & Designed by Felix © 2012