如何通过IAlertNotifyHandler来自定义Alert Email

问题描述

==========

自定义Alert email, 不仅仅修改外观, 我们需要对内容的输出进行控制

 

解决方案

==========

使用IAlertNotifyHandler接口来制作邮件内容, 修改邮件内容

 

步骤

==========

1. 创建一个class library工程, 完整代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;


namespace AlertNotifyHandler
{
    public class Class1 : IAlertNotifyHandler
    {
        #region IAlertNotifyHandler Members
        
        public bool OnNotification(SPAlertHandlerParams ahp)
        {
            try
            {
                SPSite site = new SPSite(ahp.siteUrl + ahp.webUrl);
                SPWeb web = site.OpenWeb();
                SPList list = web.Lists[ahp.a.ListID];
                SPListItem item = list.GetItemById(ahp.eventData[0].itemId);
                string eventType = string.Empty;


                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)    
            {
                FileStream fs = new FileStream(@"C:\temp\alertnotifyhandler.log", FileMode.Create);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(ex.ToString());

                return false;
            }
        }
        #endregion
    }
}

 

2. 为工程添加签名(添加一个snk文件). Build.

3. 部署dll到SharePoint Server的GAC中.

4. 复制C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\XML\alertTemplates.xml文件. 永远不要修改原文件!

5. 重命名复本为CustomAlertTemplates.xml, 保存.

6. 编辑文件, 在文件中搜索关键字"Properties". 你会搜索到像下面这样的部分.

<Properties>
    <ImmediateNotificationExcludedFields>ID;Author;Editor;...省略...;NumComments;</ImmediateNotificationExcludedFields>
    <DigestNotificationExcludedFields>ID;Author;Editor;...省略...;NumComments;</DigestNotificationExcludedFields>
</Properties>

修改这个部分为:

<Properties>
    <ImmediateNotificationExcludedFields>ID;Author;Editor;...省略...;NumComments;</ImmediateNotificationExcludedFields>
    <DigestNotificationExcludedFields>ID;Author;Editor;...省略...;NumComments;</DigestNotificationExcludedFields>
          <NotificationHandlerAssembly>AlertNotifyHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=dfeb24947a0729b6</NotificationHandlerAssembly>
          <NotificationHandlerClassName>AlertNotifyHandler.Class1</NotificationHandlerClassName>
          <NotificationHandlerProperties></NotificationHandlerProperties>
</Properties>

 

7. 注意, 现在你只修改了一种Alert email的输出. 附近的这个语句指出了你修改的alert的类型:

<AlertTemplate Type="List"  Name="SPAlertTemplateType.DocumentLibrary">

按照你的需要, 搜索你想要修改的alert类型, 并在相应的部分进行修改.

 

8.运行这个命令:

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN:  stsadm -o updatealerttemplates -filename "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\XML\customalerttemplates.xml" -url <your sharepoint site url>

 

9. 为了更快的看到alert email修改的结果, 运行下面的这个命令. 注意, 成功之后要记得改回来.

Run this command:  stsadm -o setproperty -pn job-immediate-alerts -pv "every 1 minutes" so that you can see the log file come back in one minute.

 

10. 确保SharePoint 服务器配置了outgoing email.

11. 确保你已经在文档库上开启了alert.

12. IISRESET

13. 重启Windows SharePoint Services Timer 服务.

 

问题解决. 文中代码已经通过了作者的测试.

 

另外, 在这里记录一个相关问题

=============

运行了这个命令之后, 整个服务器场内所有的web application都会受到该命令的影响, 即一个web application的email alert改过之后, 就场内的所有站点的alert email的模板就全都跟着变了.

你可能会问, 我明明指定了一个URL的呀. 呵呵, 这个问题只有微软才知道为什么会这样却不去修复它, 可能改动太大的缘故吧. 但是命令需要制定一个站点的URL还是有点容易迷惑人.

如果你感兴趣的话, 你可以在config DB, content DB和上抓profiler trace, 你会看到你的命令的修改最终反映到的不是content db, 而是config db.

 

参考资料:

How To: Customizing alert emails using IAlertNotifyHandler

http://blogs.msdn.com/sharepointdeveloperdocs/archive/2007/12/14/how-to-customizing-alert-emails-using-ialertnotificationhandler.aspx

Customizing Alert Notifications and Alert Templates in Windows SharePoint Services 3.0

http://blogs.msdn.com/sharepointdeveloperdocs/archive/2007/12/07/customizing-alert-notifications-and-alert-templates-in-windows-sharepoint-services-3-0.aspx

posted on 2010-05-22 16:42  中道学友  阅读(612)  评论(0编辑  收藏  举报

导航

技术追求准确,态度积极向上