How To Create Alerts Programmatically

This following piece of code will show you how to programmatically create a SharePoint alert:
First I will show you the code snippet to create the Alert, after that I will explain the code step by step.
SPAlert newAlert = user.Alerts.Add(); newAlert.Title = list.Title; newAlert.AlertType = SPAlertType.List; newAlert.List = list; //newAlert.EventType = SPEventType.Add; <= doesn’t work //use the following codes to set the "eventtypeindex" property //all =0, added = 1, modify = 2, deleted = 3, web discussions = 4 newAlert.Properties["eventtypeindex"] = "1"; newAlert.AlertFrequency = SPAlertFrequency.Immediate; //passing false to Update method will refrain from sending the alert confirmation mail newAlert.Update(false);
Step by step:
The first thing to create the alert is to use the Add method of the Alerts property of the SPUser class, in our example “user” is an instance of a SPUser class.
SPUser user = mySite.Users["domain\user"]; SPAlert newAlert = user.Alerts.Add();
If you want to set an alert for a list, you have to set the AlertType property to SPAlertType.List and set the List property to an instance of the SPList class.
newAlert.AlertType = SPAlertType.List; newAlert.List = list;
If you want to set an alert for a list item, you have to set the AlertType property to SPAlertType.Item and set the Item property to an instance of the SPListItem class.
newAlert.AlertType = SPAlertType.Item; newAlert.Item = item;
The next property you want to set is the EventType property, this didn’t work out the way I wanted it too. I was having a problem with setting the EventType on an SPAlert to SPEventType.Add.
My code wouldn’t throw an error but neither would it set the EventType to SPEventType.Add, in stead it would stay at SPEventType.All.
I fixed this by using the following codes to set the “eventtypeindex” property of the SPAlert:
- all = 0
- added = 1
- modify = 2
- deleted = 3
- web discussions = 4
//newAlert.EventType = SPEventType.Add; newAlert.Properties["eventtypeindex"] = "1";
The last properties to set is the AlertFrequency.
newAlert.AlertFrequency = SPAlertFrequency.Immediate;
If you AlertFrequency is different than Immediate you will have to set the AlertTime property, the AlertTime property defines the next time the alert will be sent.
newAlert.AlertFrequency = SPAlertFrequency.Daily; newAlert.AlertTime = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 8, 0, 0);
Last but not least you will have to call the Update method of the instance of SPAlert. You can pass a bool parameter which tells the update if it needs to send an alert confirmation mail or not.
newAlert.Update(false);
No related posts.
If you like this post then please consider subscribing to my full feed RSS. You can also subscribe by Email and have new posts sent directly to your inbox.
March 3rd, 2009 at 16:09
It seems that no matter what I do, it won’t add it to the user’s subscriptions.
I’ve tried calling update on all three involved objects (User, Alert, Web) but it seems like nothing will make the event show up in their Alert subscription management page.
Is there anything else?
April 5th, 2009 at 06:03
[...] can one do this in SharePoint? Navigate here and download the code and explanation. The how to was the work of Erwin who explains his method in [...]
April 10th, 2009 at 09:29
A few things to mention,
Do you have enough rights to add alerts for other users on the site/list/item your are trying this, try to do this by using the web interface of SharePoint to add an alert for someone else.
April 28th, 2009 at 14:38
Hi I have a little question. One of the columns in my listitem is a lookupfield. i’ve set up a mailserver to send the alerts which works fine when I update other columns. I get an email specifying that something has changed. However, when the lookupfield changes. I do not get alert.
Is there anything special going on with lookupfields that would not trigger an alert?? and what could I do to get the alert working when a loopkupfield changes??
May 12th, 2009 at 01:01
[...] How To Create Alerts Programmatically | Sharepointology (tags: programming sharepoint alerts) [...]
May 19th, 2009 at 11:35
This post is very useful for me. I have a question here. Can anybody tell me how to pass parameters to AlertTime property to set AM or PM timings
Thanks in advance
Reetha
July 24th, 2009 at 06:31
[...] Here is a good post on more options when sending out an alert: http://www.sharepointology.com/development/how-to-create-alerts-programmatically/ [...]
August 19th, 2009 at 02:56
[...] I recently ran into a problem creating alerts in a web part for the current user. After the alert was added, no matter what I set the SPAlert’s EventType to, it always defaulted to All. I finally figured out a way around this problem thanks to a helpful post by erwin at sharepointology. [...]
February 4th, 2010 at 19:08
Hi ervin today full time i spent for this. Now i am at home my house. I hope this is what i want.. Any thanks for your sharing ….. All the best for your future endeavor.
Thanks
Murali kumaran.
February 8th, 2010 at 20:26
Guys, If you are subscribing alerts for some other user, you should be the administrator or should have permisssions to manage alerts.
Else run the code with
SPSecurity.RunWithElevatedPrevilages(delegate()
{
web.AllowUnSafeUpdates=true;
/Here comes the code
});