locked
Sharepoint Alerts on List Folders / Sub Folders

    Question

  • Hi,

    I would like to programatically add alert to folders inside a sharepoint list. I have found how to set alerts to a list and this works perfect.

    Could someone please help me on how to set alerts to a specific folder which is inside a list.

    Below is the code i currently have which sets alerts only to the list.

    using (SPSite site = new SPSite(http://site/)){
    using (SPWeb web = site.OpenWeb())
    {   
    SPUser user = web.SiteUsers["domain\\user"];
    SPAlert newAlert = user.Alerts.Add();
    newAlert.AlertType = SPAlertType.List;
    newAlert.List = web.Lists["Documents"];
    newAlert.EventType = SPEventType.All;
    newAlert.AlertFrequency = SPAlertFrequency.Immediate;
    //passing true to Update method will send alert confirmation mail
    newAlert.Update(

    true);

    Your help will be much appreciated
    Thank You!

    • Moved by Mike Walsh FIN Wednesday, October 14, 2009 11:15 AM prog q (From:SharePoint - General Question and Answers and Discussion)
    • Edited by i_tech Wednesday, October 14, 2009 11:20 AM code formatting
    Wednesday, October 14, 2009 11:05 AM

Answers

  • This works.

     public static void AddFolderAlerts()
     {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
    
                    using (SPSite site = new SPSite("http://basesmcdev2/sites/tester1"))
                    {
    
                        using (SPWeb web = site.OpenWeb())
                        {
    
                            SPUser user = web.Users["basesmcdev2\\steve.curran"];
                            SPFolder folder = web.GetFolder("http://basesmcdev2/sites/tester1/Shared Documents/newfolder");
                            user.Alerts.Add(folder.Item, SPEventType.All, SPAlertFrequency.Immediate);
                            user.Update();
    
                        }
    
                    }
    
                });
    
    
    }

    certdev.com
    Wednesday, October 14, 2009 3:51 PM

All replies

  • This works.

     public static void AddFolderAlerts()
     {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
    
                    using (SPSite site = new SPSite("http://basesmcdev2/sites/tester1"))
                    {
    
                        using (SPWeb web = site.OpenWeb())
                        {
    
                            SPUser user = web.Users["basesmcdev2\\steve.curran"];
                            SPFolder folder = web.GetFolder("http://basesmcdev2/sites/tester1/Shared Documents/newfolder");
                            user.Alerts.Add(folder.Item, SPEventType.All, SPAlertFrequency.Immediate);
                            user.Update();
    
                        }
    
                    }
    
                });
    
    
    }

    certdev.com
    Wednesday, October 14, 2009 3:51 PM
  • Hi Steve,

    Thank you very much. but i am getting an error as below. The username i ahve is correct with the domain. please could you shed some light.

    Microsoft.SharePoint.SPException: User cannot be found.
       at Microsoft.SharePoint.SPUserCollection.get_Item(String loginName)
       at Alerts_Programatically.Program.<AddFolderAlerts>b__0() in C:\Inetpub\wwwro
    ot\.......\Program.cs:line 31

    Many Thanks
    • Proposed as answer by iUser_ Thursday, October 15, 2009 2:00 PM
    • Unproposed as answer by Mike Walsh FIN Thursday, April 07, 2011 11:26 AM
    Wednesday, October 14, 2009 4:19 PM
  • You can try sticking to your original code getting the user from Web.SiteUsers and see if that still works.
    certdev.com
    Wednesday, October 14, 2009 5:13 PM
  • This works perfect! Thank You so much Steve!!

    God Bless!
    Thursday, October 15, 2009 8:54 AM
  • Great Post!
    Thursday, October 15, 2009 2:00 PM