Wednesday, May 2, 2012

Creating SharePoint alert programmatically

The class used to create an alert and generate periodic email, sms to the intended user of the sharepoint site, this alert are related to creation, addition, modification, deletion ect on the list or the document libraries.

We can use following codes to set the “eventtypeindex” property of the SPAlert to specify on what action the alert is generated or created:
  • all = 0
  • added = 1
  • modify = 2
  • deleted = 3
  • web discussions = 4
which can be specified as follows :


//newAlert.EventType = SPEventType.Add;

newAlert.Properties["eventtypeindex"] = "1";



So following is the complete code for creating an alert


SPWeb spWebObject = SPContext.Current.Web;
SPUserCollection spCollUsersObject = spWebObject.Users;
 
    foreach (SPUser spUserObject in spCollUsersObject)
    {
        SPAlertCollection spCollalertsObject = spUserObject.Alerts;
 
         foreach (SPAlert spAlertObject in spCollalertsObject)
         {
         
            spAlertObject.Properties["eventtypeindex"] = "1"; //this can be 0,1,2,3,4 according to above option
             spAlertObject.AlertFrequency = Microsoft.SharePoint.SPAlertFrequency.Immediate;
             spAlertObject.Update(); //or -->  Alert.Update(false);//disable email //notification to users
             
             
             //if you need to set the frequency for date time you can add the following option
             
             /*
             spAlertObject.AlertFrequency = SPAlertFrequency.Daily;
             spAlertObject.AlertTime = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 8, 0, 0);
             */
         }
     }
Checkout the following videos for more clarity and understanding :





No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

#Intel #Core #i9 #Processors: A Breakdown

Intel Core i9 Processors: A Breakdown Intel's 14th Gen Core i9 series offers a range of processors designed for various use cases. Her...