Create a new alert for a SharePoint list
A code snippet for creating a new alert for a SharePoint list. The OpenSite() method simply creates an SPSite object named which I’ve named _site.
Make sure that your alertUser has permissions to the list. Also you need to set the property AlwaysNotify = false. For some reason it doesn’t seem to work if you don’t set this property.
public void CreateAlert(SPList list, SPUser alertUser, string alertTitle, bool notify)
{
OpenSite();
try
{
SPWeb web = _site.AllWebs[list.ParentWebUrl];
SPAlertCollection alerts = web.Alerts;
// get the lists alert template and apply that to the new alert
SPAlertTemplate alertTemplate = list.AlertTemplate;
alertTemplate.Name = list.AlertTemplate.Name;
// setup the alert
SPAlert alert = alerts.Add();
alert.AlertType = SPAlertType.List;
alert.AlertTemplate = alertTemplate;
alert.Title = alertTitle;
alert.EventType = SPEventType.Add;
alert.AlertFrequency = SPAlertFrequency.Immediate;
// make sure to set this to false. Don’t know why but doesn’t seem to work if you don’t.
alert.AlwaysNotify = false;
alert.User = alertUser;
alert.List = list;
alert.Update(notify);
// cleanup
web.Close();
web.Dispose();
}
catch
{
Console.WriteLine("Unable to create alert");
}
CloseSite();
}
0 comments
Kick things off by filling out the form below.
Leave a Comment