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

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.

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

  11. Devanshu

    hi ervin,Thank u for this useful stuff…it was the thing for which i was searching..
    but i have another query when we add alert programmatically they are not shown in the list like my alert..to display those what need to be done …can anybody help me on this…

  12. Kelly Ford

    Great post. Worked perfectly. Thank you very much.

  13. mahmoud

    thnx for this great article ,
    i have a question , can i create 1 alert on more than 1 lists (to send all notifications in one email) ???

  14. Changing SPAlert EventType | Abdel-Rahman.IT

    [...] Still the user gets the alerts whenever any new item is added to the list, which drove me crazy some long couple of search hours, I found that blog post [...]

  15. Jon

    I have a few questions about this. Can I use this to provide another option in the “Alert Me” button besides “Set alert on this list”?? How do Implement the code above to add the new alert?

    I’m asking because I need the ability to (per user) have some custom code execute whenever something on my list changes.

  16. Mark Kamoski

    Dear Erwin –

    Please help.

    Regarding creating SharePoint Alerts Programmatically, I saw your article here…

    http://www.sharepointology.com/development/how-to-create-alerts-programmatically/

    …and it was quite helpful so thank you for that…

    …however, I still need to know how to set the “Send Alerts for These Changes” options programmatically…

    …so do you know how to set this option using the SharePoint Object Model programmatically in a Web Part…

    Send Alerts for These Changes
    Send me an alert when:
    - Anything changes
    - Someone else changes an item
    - Someone else changes an item created by me
    - Someone else changes an item last modified by me
    - Someone changes an item that appears in the following view: (DropDownList)

    …???

    Please advise.

    Thank you.

    - Mark Kamoski

  17. sudheer

    Hi,

    I tried the above example…it worked without any errors…but it is not showing any programmatically created alerts in my alert subscription management page…How can i know that alerts are configured?

    Thanks,
    PVSAVSG.

  18. Larry W Virden

    I have a case where I want to delete an alert programatically. The issue is this – the alert was created by a workflow, and the workflow (in fact the site holding the workflow) has been deleted for some time now. It cannot be restored.

    A user continues to get email saying that he has been assigned a task that is overdue. We need some way to delete the alert so that it no longer keeps firing.

  19. SharePoint 2010: Cómo seguir los cambios producidos en un sitio (I)! - Blog del CIIN

    [...] http://www.sharepointology.com/development/how-to-create-alerts-programmatically/ [...]

  20. Mike

    if there are not reasons to create custom sharepoint alerts with annoying programming, the following tool may help you: http://www.harepoint.com/Products/HarePointCustomAlerts/Default.aspx

  21. Logitech - LS21

    Better you connect another mouse with your computer.
    Recently many players encounter G27 problem in Shift 2: Unleashed.
    Building a better Naga – I’ve always really liked Razer’s Naga (and I’ve used nearly all of them at some point), but I have to say that the G600 is hands-down my pick.

  22. internetradio aufnehmen

    The Squeezbox Boom radio from Logitech is not just an Internet radio.

    Helix Server Basic Free streaming media server software which can distribute live and on-demand video and other
    media. The banner ad may be placed on the same page listeners go to when they want to listen to the station on-line.

  23. Nathan

    This connectivity will allow you to easily transfer photos on to your laptop
    for storage. It includes the feature of red eye reduction and the various white
    balancing features include Auto, Daylight,
    Cloudy, Fluorescent H, Fluorescent L, Tungsten and Custom.

    As you will see later other considerations like lenses, flash and battery are no less important.

  24. Allison

    During the summer of 2008 I started using internet radio to
    reach an even wider audience. The table top radio connects to the internet using Wi-Fi or Ethernet cable, and searches for stations by country, genre or call letters.
    You will also need to be sure that you have enough bandwidth from
    your web host to run the programming smoothly.

  25. http://www.silkroadonline.de/user/40197-nikischwa/

    If it is lower the Studio365 will tell you and let you know that it will not sound well.
    Up All Night Wit Brenda White can be heard on Thursday night, live, beginning at 9:00 p.
    Given the manner in which the internet and the World Wide Web have invaded our lives over the
    past many years, it is hardly surprising that music or rather the radio would be one of the areas being
    dominated by the internet.

  26. andersonville boutiques

    Howdy! Quick question that’s completely off topic. Do you know how to make your site mobile friendly? My blog looks weird when viewing from my iphone. I’m trying to find a template
    or plugin that might be able to fix this issue. If you have any
    recommendations, please share. Appreciate it!

  27. blomstrandine

    Fantastic post but I was wanting to know if you could write a litte
    more on this topic? I’d be very grateful if you could elaborate a little bit further.
    Bless you!

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