Capturing and Modifying SharePoint Alerts through Alert Handlers

This article describes how to capture and modify email alerts with alert handler in SharePoint 2010

Introduction

SharePoint facilitate users to opt alerts through email/SMS to get notified when changes occur in list/library such as item added, modified or deleted. These alerts are being sent on 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 alert handler and modify the alert template using a custom class.

Implementation

To create an alert handler we need to create a global assembly which contains a class that implements IAlertNotifyHandler interface in Microsoft.SharePoint assembly. The IAlertNotifyHandler interface contains only one method namely OnNotification through which we can intercept the e-mail alert and modify it.

Steps Involved are:

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. Either you can use a Visual Studio Class Library project template or SharePoint Project template. In this example I am using 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 Empty SharePoint Project template and name it as “SampleAlertHandler” and click OK

In the above code we are modifying the alert template inside “OnNotification” method, which is being called prior to send the alert. The SPAlertHandlerParams (ahp) contains all the information about the alert. The ahp.headers includes email properties such as from, to, Subject etc. from which we are fetching the recipient address (to). In this example, we are using title of the list (from which the alert is triggered) as subject. To construct the email body we are using ahp.eventData[0].eventType (SPEventType) property which indicates the type of alert event occurred. Finally we are sending email using SendEmail method of “SPUtility” class.

Note: Take note of the Version and Public Key Tokensince we require these while creating alert template.

Create an Alert Template

Alert templates are XML files which defines content and appearance of both e-mail (alerttemplates.xml) and SMS (alerttemplates_sms.xml) alerts. Alert templates are located in “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\XML” folder.

In this example we only deal with e-mail alert template (alerttemplates.xml). If you open the “alerttemplates.xml” you can see that there are 16(v.2010) different AlertTemplate elements defined in it for different types of list. The Name attribute of the AlertTemplate specifies the Template type.

Now we can create an alert template for our alert handler.

Step 1: Move to 14 hive “TEMPLATE\XML” folder and make a copy of alerttemplates.xml and name it as “custom_alerttemplates.xml”.

Note: It is recommended to keep the original templates intact because any future SharePoint update will overwrite your changes.

Step 2: Open the “custom_alerttemplates.xml” in Visual Studio or any other XML editor and locate AlertTemplate element which is defined for DocumentLibrary template type (since we are using document library to test our notification handler in this example).

Step 5: Restart IIS, to do this run iisreset command in a command window.

Step 6: As a final step we need to restart SharePoint 2010 Timer service (as it performs the tasks such as sending alerts). To do this run services.msc command in the command window which will open the Services Window where locate the “SharePoint 2010 Timer Service” and restart.