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

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

Article ID : 948321
Last Review : March 26, 2008
Revision : 1.1

SUMMARY

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 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 this article describes.
• 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.

MORE INFORMATION

This method creates a class that inherits from the IAlertNotificationHandler interface and that uses the OnNotification method. This method enables you to 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:

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

• Installing an Assembly into the Global Assembly Cache
http://msdn2.microsoft.com/en-us/library/dkkx7f79(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/dkkx7f79(vs.71).aspx)
• Global Assembly Cache Tool (Gacutil.exe)
http://msdn2.microsoft.com/en-us/library/aa309379(VS.71).aspx (http://msdn2.microsoft.com/en-us/library/aa309379(VS.71).aspx)

For more information, click the following article number to view the article in the Microsoft Knowledge Base:

315682 (/Feedback.aspx?kbNumber=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=1.0.0.0, 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=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 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 specified is in minutes. For this test, the frequency is set to 1 minute.

For more information, visit the following Microsoft Web site:

http://technet2.microsoft.com/windowsserver/WSS/en/library/0eb072fe-8321-483b-9d1e-3412e3f42a481033.mspx?mfr=true (http://technet2.microsoft.com/windowsserver/WSS/en/library/0eb072fe-8321-483b-9d1e-3412e3f42a481033.mspx?mfr=true)
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:

http://technet2.microsoft.com/windowsserver/WSS/en/library/91570494-09ba-4537-904b-c61a6268d6bc1033.mspx?mfr=true (http://technet2.microsoft.com/windowsserver/WSS/en/library/91570494-09ba-4537-904b-c61a6268d6bc1033.mspx?mfr=true)
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.


APPLIES TO
• Microsoft Office SharePoint Server 2007
• Microsoft Office SharePoint Server 2007 for Search (Enterprise Edition)
• Microsoft Office SharePoint Server 2007 for Search (Standard Edition)

Back to the top

Keywords: 
kbhowto kbexpertiseadvanced kbinfo KB948321

 

Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
Microsoft Corporation. All rights reserved. Terms of Use | Trademarks


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply

*
To prove that you're not a bot, enter this code
Anti-Spam Image