如何在SharePoint2010中创建自定义电子邮件警报处理程序

  • 字段,如项目名称字段中,将被截断到的电子邮件通知中的 70 个字符。要解决 70 个字符的限制,请使用"更多信息"一节中的介绍的方法。
  • 要嵌入电子邮件通知中的其他内容。
  • 您想要更改的布局或外观的电子邮件通知。

此方法创建一个类,从IAlertNotificationHandler接口继承,并使用OnNotification方法。此方法可以截获传出的电子邮件通知,并对其进行修改。您可以访问该警报的属性中的大多数。通过使用 XML 分析和 SharePoint 对象模型的代码,您可以提取必须具有修改电子邮件警报的所有信息。然后,您可以生成 HTML 存根以显示根据您的要求的电子邮件通知。此外,您可以通过使用 SharePoint 的 SendMail 功能发送电子邮件通知。
这些步骤包括输出以接近于默认通知模板的电子邮件格式设置的示例代码。您可以修改此示例代码中的 HTML,以自定义生成的电子邮件通知。

  1. 创建一个项目,从IAlertNotificationHandler接口继承。在项目中包括 Microsoft.SharePoint 命名空间和 Microsoft.SharePoint.Utilities 的命名空间。
    使用以下代码:

    //===================code start=====================
    public class Class1:IAlertNotifyHandler
    {
    
    #region IAlertNotifyHandler Members
    
    public bool OnNotification(SPAlertHandlerParams ahp)
    {
    SPSite site = null;
    SPWeb web = null;
    
    try
    {
    site = new SPSite(ahp.siteUrl+ahp.webUrl);
    web = site.OpenWeb();
    SPList list=web.Lists[ahp.a.ListID];
    SPListItem item = list.GetItemById(ahp.eventData[0].itemId) ;
    
    string FullPath=HttpUtility.UrlPathEncode(ahp.siteUrl+"/"+ahp.webUrl+"/"+list.Title+"/"+item.Name);
    string ListPath = HttpUtility.UrlPathEncode(ahp.siteUrl + "/" + ahp.webUrl + "/" + list.Title);
    string webPath=HttpUtility.UrlPathEncode(ahp.siteUrl+"/"+ahp.webUrl);
    
    string build = "";
    if (ahp.eventData[0].eventType==1)
    eventType="Added";
    else if(ahp.eventData[0].eventType==2)
    eventType="Changed";
    else if(ahp.eventData[0].eventType==3)
    eventType="Deleted";
    
    build = "<style type=\"text/css\">.style1 { font-size: small; border: 1px solid #000000;"+
    "background-color: #DEE7FE;}.style2 { border: 1px solid #000000;}</style></head>"+ 
    "<p><strong>"+ item.Name.ToString() +"</strong> has been "+eventType +"</p>"+
    "<table style=\"width: 100%\" class=\"style2\"><tr><td style=\"width: 25%\" class=\"style1\">"+
    "<a href="+ webPath +"/_layouts/mysubs.aspx>Modify my Settings</a></td>"+
    "<td style=\"width: 25%\" class=\"style1\"> <a href="+ FullPath +">View "+item.Name+"</a></td>"+
    "<td style=\"width: 25%\" class=\"style1\"><a href=" + ListPath + ">View " + list.Title + "</a></td>" +
    " </tr></table>";
    string subject=list.Title.ToString() ; 
    SPUtility.SendEmail(web,true , false, ahp.headers["to"].ToString(), subject,build);
    return false;
    }
    catch (System.Exception ex)
    {
    return false;
    }
    finally
    {
    if (web != null)
    web.Dispose();
    if (site != null)
    site.Dispose();
    }
    
    }
    
    #endregion
    }
    //===================code end=====================

  2. 强签名的.dll 文件添加到全局程序集缓存 (GAC) 中。您可以将该程序集拖到程序集文件夹。或者,您可以使用 GACUtil 工具来注册.dll 文件。
    有关详细信息,请访问下面的 MSDN Web 站点。 有关详细信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:

    315682

    () http://support.microsoft.com/kb/315682/

    如何将程序集安装到全局程序集缓存在 Visual Basic.NET 或 Visual Basic 2005年中

  3. 请在下面的文件夹中的 alertTemplates.xml 文件的副本:

    C:\Program 必须使用 Shared\Web 服务器 Extensions\12\Template\Xml

    命名为 CustomAlertTemplates.xml,该新文件,然后保存该文件。
    注意不要直接修改 alertTemplates.xml 文件。直接修改此文件不受支持。

  4. 编辑文件和搜索的关键字属性。在属性块中添加以下行:

    <NotificationHandlerAssembly>AlertHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d59ecf2a3bd66904</NotificationHandlerAssembly>
              <NotificationHandlerClassName>AlertHandler.Class1</NotificationHandlerClassName>
        <NotificationHandlerProperties></NotificationHandlerProperties>

    存根 (stub) 应类似于这现在:

            <Properties>
                <ImmediateNotificationExcludedFields>ID;Author;Editor;Modified_x0020_By;Created_x0020_By;_UIVersionString;ContentType;TaskGroup;IsCurrent;Attachments;NumComments;</ImmediateNotificationExcludedFields>
                <DigestNotificationExcludedFields>ID;Author;Editor;Modified_x0020_By;Created_x0020_By;_UIVersionString;ContentType;TaskGroup;IsCurrent;Attachments;NumComments;</DigestNotificationExcludedFields>
                <NotificationHandlerAssembly>AlertHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d59ecf2a3bd66904</NotificationHandlerAssembly>
                    <NotificationHandlerClassName>AlertHandler.Class1</NotificationHandlerClassName>
                    <NotificationHandlerProperties></NotificationHandlerProperties>
      </Properties>

    要在警报的模板文件中每个警报的模板部分中包括此 XML 存根 (stub)。

  5. 在命令提示符下,更改 C:\Program 必须使用 Shared\web 的服务器 extensions\12\BIN 目录。然后,运行以下命令:

    stsadm-o updatealerttemplates-文件名"是 Files\Common Files\Microsoft Shared\Web 服务器 Extensions\12\TEMPLATE\XML\customalerttemplates.xml"-url your_sharepoint_site url

  6. 运行以下命令:

    stsadm-o setproperty-pn 作业-立即-通知-pv"每隔 1 分钟"

    此命令指定 SharePoint 服务器检查都要立即发送的通知的频率。以分钟为单位指定的值。对于此测试,将频率设置为 1 分钟。
    有关详细信息,请访问下面的 Microsoft 网站:

    http://technet2.microsoft.com/windowsserver/WSS/en/library/0eb072fe-8321-483b-9d1e-3412e3f42a481033.mspx?mfr=true

    (http://technet2.microsoft.com/windowsserver/WSS/en/library/0eb072fe-8321-483b-9d1e-3412e3f42a481033.mspx?mfr=true)

  7. 请确保您拥有 SharePoint 配置传出电子邮件。有关如何配置传出电子邮件的详细信息,请访问下面的 Microsoft 网站:

    http://technet2.microsoft.com/windowsserver/WSS/en/library/91570494-09ba-4537-904b-c61a6268d6bc1033.mspx?mfr=true

    (http://technet2.microsoft.com/windowsserver/WSS/en/library/91570494-09ba-4537-904b-c61a6268d6bc1033.mspx?mfr=true)

  8. 如果测试使用文档库,请确保您有打开的文档库的通知。
  9. 运行以下命令:
    • iisreset
    • 服务
  10. 在服务 MMC 管理单元中,重新启动 Windows SharePoint Services 定时服务。
完成这些步骤之后,应配置自定义电子邮件通知的处理程序。创建一个新警报后,您将收到已更新的自定义电子邮件警报。
posted @ 2013-11-20 10:49  love007  阅读(363)  评论(0编辑  收藏  举报