rss

Sharepointology

Sharepointology is the study and handling of Microsoft Office SharePoint Server

How To Create Alerts Programmatically

Posted 18Feb09 by Erwin in Development

alert
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;

cautionThe 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.

RSSIf 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.

10 Responses

  1. A few things to mention

    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?

  2. Alert Face Off: Google vs Microsoft SharePoint : Beyond Search

    [...] 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 [...]

  3. Erwin

    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.

  4. Robert Graauw

    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??

  5. links for 2009-05-11 @ Gene & Tesha

    [...] How To Create Alerts Programmatically | Sharepointology (tags: programming sharepoint alerts) [...]

  6. Anonymous

    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

  7. Yaroslav Pentsarskyy’s SharePoint and .NET adventures » Use Alert Framework when sending notifications in your SharePoint custom app

    [...] Here is a good post on more options when sending out an alert: http://www.sharepointology.com/development/how-to-create-alerts-programmatically/ [...]

  8. Kit Menke’s Blog » Blog Archive » Creating a SharePoint with different EventTypes

    [...] 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. [...]

  9. ThiruKumaran

    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.

  10. Bharath

    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
    });

Leave a Comment

About

My name is Erwin Bastiaensen and I am a Software Architect from Belgium, with a huge interest in technology. Lately I have been involved in some Java projects and implementing, interfacing and extending a Sharepoint portal.

You can contact me at sharepointology[@]gmail.com.

View Erwin Bastiaensen's profile on LinkedIn