Microsoft Knowledge Base Email Alertz

KBAlertz.com: How to create a custom e-mail alert alert handler in Microsoft Office SharePoint Server

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

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




Article ID: 948321 - Last Review: October 20, 2008 - Revision: 2.0

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

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

MORE INFORMATION

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 Members
    
    public 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  (http://kbalertz.com/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 is specified 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)
Keywords: 
kbhowto kbexpertiseadvanced kbinfo KB948321
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please