如何修改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颇久,对于事件触发器 ...
随机推荐
- 怎么配置eclipse的联想功能
第一步:打开Eclipse,windows下,打开“window”→“Preferences” :mac下,打开“偏好设置”. 第二步:选择“Java”,展开,“Editor”,选择“Content ...
- kvm 启动libvirtd时出现错误
kvm 启动libvirtd市出现错误: /etc/init.d/libvirtd start启动 libvirtd 守护进程:libvirtd: relocation error: libvirtd ...
- apache2.2+php5.3+mysql5.5+Zend Guard Loader集成包
由前一篇文章 http://www.cnblogs.com/darktime/p/3407980.html 我就配置了一个环境包,免安装的,只需要运行一个.bat的文件文件就算安装成功了 如果你需要用 ...
- Azure 和 Linux
Azure 正在不断集结各种集成的公有云服务,包括分析.虚拟机.数据库.移动.网络.存储和 Web,因此很适合用于托管解决方案. Azure 提供可缩放的计算平台,允许即用即付,而无需投资购买本地硬件 ...
- python之系统命令
subprocess执行系统命令 call:执行命令,返回状态码 #!/usr/bin/env python # _*_ coding:utf-8 _*_ __Author__ = 'KongZhaG ...
- C#中关于增强类功能的几种方式
C#中关于增强类功能的几种方式 本文主要讲解如何利用C#语言自身的特性来对一个类的功能进行丰富与增强,便于拓展现有项目的一些功能. 拓展方法 扩展方法被定义为静态方法,通过实例方法语法进行调用.方法的 ...
- Elasticsearch入坑指南之RESTful API
Elasticsearch入坑指南之RESTful API Tags:Elasticsearch ES为开发者提供了非常丰富的基于Http协议的Rest API,通过简单的Rest请求,就可以实现非常 ...
- 【MySQL】sysbench压测服务器及结果解读
主要压测范围包括CPU测试.磁盘IO测试.线程测试.OLTP测试等,那么sysbench就可以满足我们的压测需求.下面我们简单来看下sysbench的安装使用以及压测结果的解读. 一.sysbench ...
- Linux 新建用户和组命令
用户的角色是通过UID和GID识别的. UID用户ID:相当于各为的身份证,在系统中是唯一的 GID组ID:相当于各为的家庭或者你们的学校. 1.新建用户及设置密码命令如下: useradd [参数] ...
- 基于php-fpm的配置详解
php5.3自带php-fpm/usr/local/php/etc/php-fpm.confpid = run/php-fpm.pidpid设置,默认在安装目录中的var/run/php-fpm.pi ...