How to create a custom e-mail alert handler in Microsoft Office SharePoint Server

This article describes how to create a custom e-mail alert handler in Microsoft Office SharePoint Server 2007. The method that is described in this article uses the IAlertNotificationHandler interface to intercept the e-mail alert and to modify it.

You may want to create a custom e-mail alert handler in Microsoft Office SharePoint Server 2007 in any of the following scenarios:
  • Fields, such as the ItemName field, are truncated to 70 characters in the e-mail alert. To work around the 70-character limit, use the method that is described in the "More information" section.
  • You want to embed additional content in the e-mail alert.
  • You want to change the layout or the appearance of the e-mail alert.
This method creates a class that inherits from the IAlertNotificationHandler interface and that uses the OnNotification method. This method lets you intercept the outgoing e-mail alerts and modify them. You can access most of the properties of the alert. By using XML parsing and SharePoint object model code, you can extract all the information that you must have to modify the e-mail alert. Then, you can build the HTML stub to display the e-mail alert based on your requirements. Also, you can send the e-mail alert by using SharePoint’s SendMail functionality.

These steps include sample code that formats the output to closely resemble the default alert template e-mail messages. You can modify the HTML in this sample code to customize the resulting e-mail alert.
  1. Create a class project that inherits from the IAlertNotificationHandler interface. Include the Microsoft.SharePoint namespace and the Microsoft.SharePoint.Utilities namespace in the project.

    Use the following code:
    //===================code start=====================public class Class1:IAlertNotifyHandler{#region IAlertNotifyHandler Memberspublic bool OnNotification(SPAlertHandlerParams ahp){SPSite site = null;SPWeb web = null;try{site = new SPSite(ahp.siteUrl+ahp.webUrl);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;}finally{if (web != null)web.Dispose();if (site != null)site.Dispose();}}#endregion}//===================code end=====================
  2. Add a strongly signed .dll file to the Global Assembly Cache (GAC). You can drag the assembly to the assembly folder. Or, you can use the GACUtil tool to register the .dll file.

    For more information, visit the following MSDN Web sites.For more information, click the following article number to view the article in the Microsoft Knowledge Base:
    315682 How to install an assembly in the Global Assembly Cache in Visual Basic .NET or in Visual Basic 2005
  3. Make a copy of the alertTemplates.xml file that is in the following folder:
    C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\Template\Xml
    Name this new file CustomAlertTemplates.xml, and then save the file.

    Note Do not directly modify the alertTemplates.xml file. Directly modifying this file is unsupported.
  4. Edit the file and search for the keyword properties.Add the following lines to the properties block:
    <NotificationHandlerAssembly>AlertHandler, Version=, Culture=neutral, PublicKeyToken=d59ecf2a3bd66904</NotificationHandlerAssembly>          <NotificationHandlerClassName>AlertHandler.Class1</NotificationHandlerClassName>    <NotificationHandlerProperties></NotificationHandlerProperties>
    The stub should resemble 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=, Culture=neutral, PublicKeyToken=d59ecf2a3bd66904</NotificationHandlerAssembly>                <NotificationHandlerClassName>AlertHandler.Class1</NotificationHandlerClassName>                <NotificationHandlerProperties></NotificationHandlerProperties>  </Properties>
    Include this XML stub in each alert template section that you want in the alert template file.
  5. At a command prompt, change to the C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN directory. Then, run the following command:
    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 the following command:
    stsadm -o setproperty -pn job-immediate-alerts -pv "every 1 minutes"
    This command specifies how frequently SharePoint Server checks for alerts that are to be sent immediately. The value is specified in minutes. For this test, the frequency is set to 1 minute.

    For more information, visit the following Microsoft Web site:
  7. Make sure that you have SharePoint configured for outgoing e-mail messages.For more information about how to configure outgoing e-mail messages, visit the following Microsoft Web site:
  8. If you are using the document library for the test, make sure that you have alerts for the document library turned on.
  9. Run the following commands:
    • iisreset
    • services.msc
  10. In the Services MMC snap-in, restart the Windows SharePoint Services Timer service.
After you complete these steps, the custom e-mail alert handler should be configured. After you create a new alert, you should receive the updated custom e-mail alert.

Article ID: 948321 - Last Review: 10/20/2008 22:28:57 - Revision: 2.0

Microsoft Office SharePoint Server 2007, Microsoft Office SharePoint Server 2007 for Search (Enterprise Edition), Microsoft Office SharePoint Server 2007 for Search (Standard Edition)

  • kbhowto kbexpertiseadvanced kbinfo KB948321