IAlertNotifyHandler.OnNotification Method
Occurs after an alert is sent and returns information about the alert notification.
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No
Syntax
'Declaration
Function OnNotification ( _
ahp As SPAlertHandlerParams _
) As Boolean
'Usage
Dim instance As IAlertNotifyHandler
Dim ahp As SPAlertHandlerParams
Dim returnValue As Boolean
returnValue = instance.OnNotification(ahp)
bool OnNotification(
SPAlertHandlerParams ahp
)
Parameters
ahp
Type: Microsoft.SharePoint.SPAlertHandlerParamsAn SPAlertHandlerParams structure that contains information about the alert.
Return Value
Type: System.Boolean
true if SharePoint Foundation 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.
Note
Include the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces in the project.
Examples
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
}