Introduction
SharePoint assists users by providing optional alerts by email/SMS to get notifications of changes in a list/library such as when an item is added, modified, or deleted. These alerts are sent using a pre-defined template. Alert handlers allow developers to do some custom action when an alert is triggered.
Note. Alert handlers are triggered only for email alerts.
Scope
In this article, we will explore how to capture email alerts with an alert handler and modify the Alert Template using a custom class.
Implementation
To create an alert handler we need to create a global assembly that contains a class that implements the "IAlertNotifyHandler" interface in Microsoft.SharePoint assembly. The IAlertNotifyHandler interface contains only one method, "OnNotification" for intercepting the e-mail alert and to modify it.
Procedure
The procedure to do that is
- Create and Deploy an Alert Notification Handler Assembly
- Create an Alert Template
Create and Deploy an Alert Notification Handler Assembly
To create an alert notification handler we need to create a public/shared assembly. You can use a Visual Studio Class Library project template or SharePoint Project template. In this example, I am using the SharePoint Project Template to avoid signing and deployment overhead. Let us start.
Step 1. Open Visual Studio 2010 and create a new project. From the New Project window select the "Empty SharePoint Project" template and name it "SampleAlertHandler" and click "OK".
Step 2. In the next window (SharePoint Customization Wizard) give your website URL and select "Deploy as a farm solution" then click the "Finish" button.
Step 3. Add a new class file to the SampleAlertHandler project with the name "CustomAlertHandler". Now open CustomAlertHandler.cs and replace the code with the following snippet:
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
namespace SampleAlertHandler
{
public class CustomAlertHandler : IAlertNotifyHandler
{
public bool OnNotification(SPAlertHandlerParams ahp)
{
try
{
using (SPSite site = new SPSite(ahp.siteUrl + ahp.webUrl))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists[ahp.a.ListID];
string to = ahp.headers["to"].ToString();
string subject = list.Title.ToString();
string body = string.Empty;
var eventType = (SPEventType)ahp.eventData[0].eventType;
if (eventType == SPEventType.Add)
{
body = "An item is added";
}
else if (eventType == SPEventType.Modify)
{
body = "An item is modified";
}
else if (eventType == SPEventType.Delete)
{
body = "An item is deleted";
}
SPUtility.SendEmail(web, true, false, to, subject, body);
}
}
return true;
}
catch (Exception ex)
{
return false;
}
}
}
}
In the code above we are modifying the Alert Template inside the OnNotification method, that is being called prior to sending the alert. The SPAlertHandlerParams (app) contains all the information about the alert. The app. headers include email properties such as from, to, Subject etcetera from which we are fetching the recipient address (to). In this example, we are using the title of the list (from which the alert is triggered) as the subject. To construct the email body we are using app.eventData[0].eventType (SPEventType) property that indicates the type of alert event that occurred. Finally, we are sending an email using the SendEmail method of the "SPUtility" class.



Joseph AlmenarioPosted Mar 4, 2021, 11:20 AM
Delete is not firing email alert
Sreejith GopinathanPosted May 20, 2013, 2:23 AM
Thanks Shiju :)
Shiju JosephPosted May 20, 2013, 12:34 AM
Really interesting one Sreejith, Thanks for sharing
Sreejith GopinathanPosted May 19, 2013, 11:12 AM
Thanks Dinesh
Dinesh BeniwalPosted May 19, 2013, 5:23 AM
Good start, welcome to the c# corner sreejith.