如何修改sharepoint中alert发送邮件模板
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发送邮件模板的更多相关文章
- 修改vs17中的cordova模板
因为visual studio 2017创建的默认cordova-ios的版本自动编译带有swift语言的插件会出现异常,cordova-ios升级到4.3.1,并且配置build.json能解决问题 ...
- 如何修改SharePoint列表条数等阈值
若要修改SharePoint中对列表最大条数等设定的阈值,可按如下步骤操作: 1. 打开页面:管理中心 > 应用程序管理 > 管理Web应用程序.2. 选择要修改阈值的Web应用程序,并在 ...
- 不修改模板的前提下修改VisualState中的某些值
原文链接:不修改模板的前提下修改VisualState中的某些值 - 超威蓝火 UWP里有一件非常令人不爽的事,大部分控件只提供了Normal状态下的Background,Foreground,Bor ...
- 为 MDS 修改 SharePoint 2013组件
了解如何修改 SharePoint 项目中的组件以在 SharePoint 2013 中利用最少下载策略(MDS). 本文内容 为何修改 SharePoint 组件? 母版页 ASP.NET 页面 ...
- 整理 PHPstorm实用个人配置,修改调整个性化快捷键,修改使用phpstorm创建的模板的默认注释:
对你有助请点赞,请顶------送人玫瑰,手留余香! 1:58 2016/3/12 整理PHPstorm实用个人配置,修改调整个性化快捷键,修改使用phpstorm创建的模板的默认注释: PHPsto ...
- 在SharePoint中创建可自定义属性的文件夹
概况 阅读时间:约5分钟 适用版本:SharePoint Server 2010及以上 面向用户:普通用户.管理员.开发人员 难度指数:★★★☆☆ SharePoint中的文件夹分为2种,一种是文档库 ...
- Win 10 开发中Adaptive磁贴模板的XML文档结构,Win10 应用开发中自适应Toast通知的XML文档结构
分享两篇Win 10应用开发的XML文档结构:Win 10 开发中Adaptive磁贴模板的XML文档结构,Win10 应用开发中自适应Toast通知的XML文档结构. Win 10 开发中Adapt ...
- 在express站点中使用ejs模板引擎
在express站点中使用ejs模板引擎 文/玄魂 目录 在express站点中使用ejs模板引擎 前言 1.1 安装 1.2修改app.js 1.3创建测试页面 前言 使用 vs创建 ...
- SharePoint 中关于event receivers的讨论
今天一早,跟几个小伙伴在群里讨论了有关事件触发器的东西,感觉收获颇多,拿出来和大家分享.讨论的内容,主要就是关于事件触发器的同步/异步的设置以及作用. 其实接触SharePoint颇久,对于事件触发器 ...
随机推荐
- iphone使用linux命令apt-get也没有问题
那么教程开始: 首先安装cydia这个越了yu就有自带的哦 然后添加源,比如apt.91.我忘了,大家可以在http://frank-dev-blog.club/?post=45找一个 查找termi ...
- 读书笔记week1——涂涵越
这次读书笔记主要是就<程序员修炼之道>这本书的前半部分做一些总结以及发表一些自己的看法. 本书前面的一部分主要是一些程序员应该在工作中时刻注意的事情,一些关键的信息如下: 1.处理问题的态 ...
- 1.创建maven 项目 动态web工程完整示例
注意,以下所有需要建立在你的eclipse等已经集成配置好了maven了,说白了就是新建项目的时候已经可以找到maven了 没有的话需要安装maven 一.创建项目 1.新建maven项目,如果不在上 ...
- Linux echo命令详解
echo :输出文字到控制台 -n: 不换行输出 -e:解析转移字符 (-b: 退格 -n 换行 -t 空格) 常用的命令展示 echo {1..4} ==> seq -s " ...
- Microsoft Teams 版本与语音落地介绍
我亲爱的小伙伴们,失踪人口回归啦~~ 今天和大家聊的是Microsoft Teams,Teams作为协作的集大成者,可以实现skype for Business的所有功能,还可以在teams里集合Of ...
- 铁乐学python_day05-作业
1,有如下变量(tu是个元祖),请实现要求的功能 tu = ("alex", [11, 22, {"k1": 'v1', "k2": [&q ...
- 企业大数据之Elasticsearch的搜索类型
下面的 ES基于版本(V2.3.4) ES之默认 1.默认自动发先同一局域网的所有集群节点 2.默认一个索引库会有5个分片,(分片越多,效率越好) 由于这两个默认,所以统一索引库的分片对分布在不同机器 ...
- 处理Account locked due to 217 failed logins的问题
处理Account locked due to 217 failed logins的问题 [root@xxx1 ~]# scp 123.txt root@IP地址:/root Account lock ...
- Android进阶笔记18:Android 获取Button的高度、宽度、坐标值
1. 问题 如何获取一个控件的长和高,相信很多朋友第一眼看见这个问题都会觉得很简单,直接在onCreate里面调用getWidth.getMeasuredWidth不就可以获得了吗,但是,事实上是并没 ...
- Java并发编程--4.Executor框架
简介 Executor框架是启动,管理线程的API, 它的内部实现是线程池机制,它有很多好处,比如使任务提交和任务执行解耦合,防止this逃逸:它的主要API包括: Executor, Execut ...