Thursday, January 21, 2010

SharePoint Alert customization

Referred URL

You can create one or more alerts for a list or document library in a SharePoint site. When creating alerts you have option of what event and when to get notified. Alerts can be managed by Administrators or Users. SharePoint object model provides methods to manage Alerts programmatically.
SPAlert class provides details on alert like alert frequency, alert type,user who created the alert. This class also provides a method to update alert details. Following code snippet shows how to create SharePoint Alert programmatically:
using (SPSite site = new SPSite("http://servername"))
using (SPWeb web = site.OpenWeb())
SPUser user = web.SiteUsers["domain name\\username];
SPAlert newAlert = user.Alerts.Add();
newAlert.AlertType = SPAlertType.List;
newAlert.List = web.Lists["List Name"];
newAlert.EventType = SPEventType.All;
newAlert.AlertFrequency = SPAlertFrequency.Immediate;
//passing true to Update method will send alert confirmation mail

SharePoint stores the alert information like web application URL and List URL in content database. Because of this, the links inside alert mail will break if web application URL or List URL is changed. To fix this user need to update the alert manually from alert manager or Administrator can run a script to update alerts. Refer this link for more details on this issue.

Alert mails can be customized in two ways:
Approach 1
You can customize the Alert tempaltes of WSS. Alert tempaltes are located in
"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\Template\XML\AlertTemplates.xml"
Following link has lot of details on this approach:

Approach 2
You can also customize alert mail format by implementing IAlertNotifyHandler interface. Following Microsoft KB article has details on this:

How to debug custom alert handlers
Alerts are generated by WSS timer job. Time job reads the event log and sends the alert. So to debug custom alert handlers you need to attach timer process to debugger (OWSTIMER.exe).

No comments: