Ads

Saturday, 23 March 2013

C# code to create alerts in document library & List programmatically

SharePoint gives some inbuilt functionality to create and manage alerts in lists & Libraries by using alert me option. It can also be done by code as shown below:
using (SPSite oSite = new SPSite("http://servername"))
{
  using (SPWeb oWeb = oSite.OpenWeb())
  {
    SPUser oUser = oWeb.SiteUsers["domain name
\\username"];
    SPAlert oListAlert = oUser.Alerts.Add(); 
    //Define the type of object to which alert is applied
    oListAlert.AlertType = SPAlertType.List;  
    //Gets or sets the List or Document Library to which alert is applied
    oListAlert.List = oWeb.Lists["List_Name"];
    //Define the Event type to which alert is applied
    oListAlert.EventType = SPEventType.All;
    //Set the time interval for sending alert.
    oListAlert.AlertFrequency = SPAlertFrequency.Immediate; 
    //Passing true to Update method will send alert confirmation mail 
    oListAlert.Update(false); 
    //Dispose unused objects                 
    oListAlert = null;
    oUser = null;
  }
}

No comments:

Post a Comment

Ads