In my post last week I talked about customizing alert notifications and alert templates. Sometimes you need to go further and create an alert handler.  This week I’d like to share a code sample from Rashid Aga, one of our escalation engineers.  His sample demonstrates how to intercept and modify alert mails using the IAlertNotifyHandler interface.

Problem:

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

There are issues with particular field names like ItemName which will be truncated in an alert email at 70 characters. There can also be situations where you want to embed additional content in the email or change the layout and email appearance altogether.

Solution:

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

Make use of the IAlertNotifyHandler interface to intercept the email and modify it.

We can create our own class that inherits from the IAlertNotifyHandler interface and uses the OnNotification method. This will allow you to intercept the outgoing alert emails and modify them. We can access most of the properties for the alert and with some xml parsing and SharePoint object model code, we can extract all the information we need to build up the email. We can then construct the HTML stub to display the email based on your requirements and send the email out using SharePoint’s SendMail functionality.

Steps:

I have included the sample code below along with the steps to set up the scenario. I have formatted the output of my code to resemble the default alert template emails as close as possible, you can customize it further to suit your needs.

1      Create a class project that inhertits from the IAlertNotifyHandler interface. Include the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces in the project.

This is the code for the class:

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 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;

}

}

#endregion

}

2.            GAC the dll.

3.            Make a copy of the alertTemplates.xml file found at this location: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\XML. Always work with a copy of AlertTemplates.xml, not the original.

4.            Call this new file CustomAlertTemplates and save the file. Edit the file and search for the keyword properties:

Include these additional lines into the properties block:

<NotificationHandlerAssembly>AlertHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d59ecf2a3bd66904</NotificationHandlerAssembly>

<NotificationHandlerClassName>AlertHandler.Class1</NotificationHandlerClassName>

<NotificationHandlerProperties></NotificationHandlerProperties>

The entire stub should look like this now:

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

Include this xml stub in each alert template section you want in the alert template file.

5.            Run this command from 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>

6.            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. Be sure to set the time back after testing.

7.            Make sure you have SharePoint already configured for outgoing emails.

8.            Make sure that you have alerts for the document library turned on if you are testing with the document library.

9.            Run this command from the command prompt: iisreset

10.         Run this command from the command prompt: services.msc

11.         From the services window, restart the Windows SharePoint Services Timer.

Your custom email alert handler should be configured at this point. Create a new alert and you should get the updated custom email.

如何修改sharepoint中alert发送邮件模板的更多相关文章

  1. 修改vs17中的cordova模板

    因为visual studio 2017创建的默认cordova-ios的版本自动编译带有swift语言的插件会出现异常,cordova-ios升级到4.3.1,并且配置build.json能解决问题 ...

  2. 如何修改SharePoint列表条数等阈值

    若要修改SharePoint中对列表最大条数等设定的阈值,可按如下步骤操作: 1. 打开页面:管理中心 > 应用程序管理 > 管理Web应用程序.2. 选择要修改阈值的Web应用程序,并在 ...

  3. 不修改模板的前提下修改VisualState中的某些值

    原文链接:不修改模板的前提下修改VisualState中的某些值 - 超威蓝火 UWP里有一件非常令人不爽的事,大部分控件只提供了Normal状态下的Background,Foreground,Bor ...

  4. 为 MDS 修改 SharePoint 2013组件

    了解如何修改 SharePoint 项目中的组件以在 SharePoint 2013 中利用最少下载策略(MDS).   本文内容 为何修改 SharePoint 组件? 母版页 ASP.NET 页面 ...

  5. 整理 PHPstorm实用个人配置,修改调整个性化快捷键,修改使用phpstorm创建的模板的默认注释:

    对你有助请点赞,请顶------送人玫瑰,手留余香! 1:58 2016/3/12 整理PHPstorm实用个人配置,修改调整个性化快捷键,修改使用phpstorm创建的模板的默认注释: PHPsto ...

  6. 在SharePoint中创建可自定义属性的文件夹

    概况 阅读时间:约5分钟 适用版本:SharePoint Server 2010及以上 面向用户:普通用户.管理员.开发人员 难度指数:★★★☆☆ SharePoint中的文件夹分为2种,一种是文档库 ...

  7. Win 10 开发中Adaptive磁贴模板的XML文档结构,Win10 应用开发中自适应Toast通知的XML文档结构

    分享两篇Win 10应用开发的XML文档结构:Win 10 开发中Adaptive磁贴模板的XML文档结构,Win10 应用开发中自适应Toast通知的XML文档结构. Win 10 开发中Adapt ...

  8. 在express站点中使用ejs模板引擎

    在express站点中使用ejs模板引擎 文/玄魂 目录 在express站点中使用ejs模板引擎 前言 1.1         安装 1.2修改app.js 1.3创建测试页面 前言 使用 vs创建 ...

  9. SharePoint 中关于event receivers的讨论

    今天一早,跟几个小伙伴在群里讨论了有关事件触发器的东西,感觉收获颇多,拿出来和大家分享.讨论的内容,主要就是关于事件触发器的同步/异步的设置以及作用. 其实接触SharePoint颇久,对于事件触发器 ...

随机推荐

  1. java注册后缀样式(ajax提示)

    方法位置 <FORM id=form1 name=form1 action = "${pageContext.request.contextPath }/user_regist.act ...

  2. opencv3.2.0形态学滤波之腐蚀

    /* 腐蚀(erode)含义: 腐蚀和膨胀是相反的一对操作,所以腐蚀就是求局部最小值的操作,腐蚀操作使原图中 国的高亮部分被腐蚀,效果图比原图有更小的高亮的区域. 腐蚀函数原型API及参数同膨胀相同 ...

  3. 计算机二进制表示、cpu架构(x86_64)、cpu频率、核心、主板

    计算机二进制表示 色彩二进制表示: 红色 255,0,0绿色 0,255,0蓝色 0,0,255 文字二进制表示:A 65a 97 cpu架构 cpu架构有精简指令集和复杂指令集两种精简指令集cpu有 ...

  4. ORACLE查询表最近更改的数据 VERSIONS BETWEEN TIMESTAMP MINVALUE AND MAXVALUE

    转自 http://blog.chinaunix.net/uid-8504518-id-3325718.html 今天开发人员问我如何快速并方便的查出一张表最近被更改的记录,这个需求很简单,由于是查最 ...

  5. webpack必知必会

    细节 url-loader和file-loader是什么关系? file-loader用于将文件路径打包为另一个url,url-loader封装了file-loader.使用url-loader时,只 ...

  6. 使用cancelBubble竟然可以阻止所有浏览器的冒泡?

    以前一直以为cancelBubble是IE8及以下的专属,今天做一个测试的时候意外发现了所有浏览器都支持,便提出来希望有哪位解释下. 1.使用原生js在FF下和chrome下两种方法都可以阻止冒泡 d ...

  7. 团队项目个人进展——Day05

    一.昨天工作总结 冲刺第五天,学习了官方文档说明,学习并但是并未实现地图的放大与缩小 二.遇到的问题 对一些框架和API不太熟悉 未实现地图的放大与缩小 三.今日工作规划 深入学习有关视频与文档说明

  8. 如何进行 Python性能分析,你才能如鱼得水?

    [编者按]本文作者为 Bryan Helmig,主要介绍 Python 应用性能分析的三种进阶方案.文章系国内 ITOM 管理平台 OneAPM 编译呈现. 我们应该忽略一些微小的效率提升,几乎在 9 ...

  9. logback.xml配置示例

    需要的jar如下: <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api< ...

  10. 附加到SQL2012的数据库就不能再附加到低于SQL2012的数据库版本

    附加到SQL2012的数据库就不能再附加到低于SQL2012的数据库版本 昨天我只是将数据库附加到SQL2012,然后各个数据库都做了收缩事务日志的操作 兼容级别这些都没有改 再附加回SQL2005的 ...