How to Use ialertpolicyhandler to customize Alert Email

Source: Internet
Author: User

Problem description

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

Custom Alert email not only modifies the appearance, but also controls the content output.

 

Solution

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

UseIalertpolicyhandlerInterface to make the mail content and modify the mail content

 

Procedure

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

1. Create a class library project. The complete code is as follows:

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>

 

2. Add a signature to the project (add an snk file). Build.

3. Deploy the dll to the gac of the SharePoint Server.

4. Copy the C: \ Program Files \ Common Files \ Microsoft Shared \ Web Server Extensions \ 12 \ TEMPLATE \ XML \ alertTemplates. xml file.Never modify the original file!

5. Rename the duplicate as CustomAlertTemplates. xml and save.

6. edit the file and search for the keyword "Properties" in the file. You will find a part like the following.

<Properties> <ImmediateNotificationExcludedFields> ID; Author; Editor ;... omitted ...; numComments; </ImmediateNotificationExcludedFields> <digesticationicationexcludedfields> ID; Author; Editor ;... omitted ...; numComments; </DigestNotificationExcludedFields> </Properties>

Modify this part:

<Properties> <ImmediateNotificationExcludedFields> ID; Author; Editor ;... omitted ...; numComments; </ImmediateNotificationExcludedFields> <digesticationicationexcludedfields> ID; Author; Editor ;... omitted ...; numComments; </strong> <icationicationhandlerassembly> alertpolicyhandler, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = custom </icationicationhandlerassembly> <icationhandlerclassname> alertpolicyhandler. class1 </NotificationHandlerClassName> <NotificationHandlerProperties> </Properties>

 

7. Note: Now you have modified only one Alert email output. The statement nearby indicates the alert type you modified:

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

Search for the alert type you want to modify according to your needs, and modify it in the corresponding section.

 

8. Run the following command:

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

 

9. To view the alert email Modification result more quickly, run the following command. Remember to change it back after the operation is successful.

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. Make sure that the SharePoint Server is configured with outgoing email.

11. Make sure that you have enabled alert on the document library.

12. IISRESET

13. RestartWindows SharePoint Services TimerService.

 

Solution: the code in this article has passed the author's test.

 

In addition, record a related question here

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

After this command is run, all web applications in the server will be affected by this command, that is, after the email alert of a web application is changed, the alert email templates of all sites in the venue have all changed.

You may ask, I have specified a URL. well, only Microsoft knows why this problem is not fixed. It may be too large to fix it. however, it is easy to confuse users to create a website URL for commands.

If you are interested, you can capture profiler trace on config DB, content DB, and you will see that the modifications to your command ultimately reflect not content db, but config db.

 

References:

How To: Customizing alert emails using ialertpolicyhandler

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

Customizing Alert configurations 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.