IAlertNotifyHandler.OnNotification Method (Microsoft.SharePoint)
Occurs after an alert is sent and returns information about the alert notification.

Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Syntax

Visual Basic (Declaration)
Function OnNotification ( _
    ahp As SPAlertHandlerParams _
) As Boolean
Visual Basic (Usage)
Dim instance As IAlertNotifyHandler
Dim ahp As SPAlertHandlerParams
Dim returnValue As Boolean

returnValue = instance.OnNotification(ahp)
C#
bool OnNotification (
    SPAlertHandlerParams ahp
)

Parameters

ahp

An SPAlertHandlerParams structure that contains information about the alert.

Return Value

true if Windows SharePoint Services marks the notification as processed; otherwise false.
Remarks

Create a class that inherits from the IAlertNotificationHandler interface and uses the OnNotification method. This allows 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. Use this information to construct the HTML stub to display the email based on your requirements and send the email out using SharePoint’s SendMail functionality.

The example formats the output to resemble the default alert template email as closely as possible, you can customize it further to suit your needs.

NoteNote:

Include the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces in the project.

Example

    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
    }
See Also

Tags :


Community Content

TomJF
Bug in SPAlertHandlerParams eventData collection?

the sample works perfect for "immediate" alert. It does not work however (at least for me) when you use it for daily or weekly summaries, and loop through the notifications with ..

SPWeb web = site.OpenWeb(ahp.webId);
SPList list = web.Lists[ahp.a.ListID];
foreach(SPAlertEventData myData in ahp.eventData)
{
if (myData.eventType == 2) // I am just interested in the change events (event type is always set correctly)
{
try
{
SPListItem item = list.GetItemById(myData.itemId);

........

Then go and define a daily alert and change some documents (checkout - change the filename of the document - checkin).

You will notice, that SharePoint somehow messes up the idemID. Instead of an ID that points to a document in the library it returns arbitrary big numbers such as 1936549236 instead of the itemID. Also the eventXML just contains "<fileds>\r\n</fields>" instad of event info.

Again, everything is OK when you just use SPListItem item = list.GetItemById(ahp.eventData[0].itemId) for an immediate alert as in the sample above.

Has anyone a clue whats wrong or can it really be that this is a bug?

Any input highly appreciated

Tom


Tao Zhang
Why the example never returns true???
This method is suppose to return true so that Windows SharePoint Services marks the notification as processed; otherwise false.
So why the example given returns false in both processed and non-processed (exception) scenarios??? And more strangely, it didn't seem to cause any damage.
We also had problem with the itemId being mixed up when there are a list of items triggering the notify event.
Tags :

Page view tracker