Freigeben über


Delete all alerts programatically

Refer to blogs.msdn.com/dwinter/archive/2005/02/15/373076.aspx for setup if you are not familiar with creating a SharePoint OM application.

This is a simple example of removal of all alerts registered in a site. This can be handy for testing or mass cleanup.

private

void button1_Click(object sender, System.EventArgs e)
{
      //delete all alerts in entire site
      using(SPSite mysite = new SPSite(this.textBox1.Text))
{
            foreach (SPWeb myWeb in mysite.AllWebs)
{
                  foreach(SPAlert myAlert in myWeb.Alerts)
{
                        try
                        {
myWeb.Alerts.Delete(myAlert.ID);
}
                        catch (Exception ex)
{
                              this.textBox2.Text = ex.Message+"\nError deleting "+myAlert.User.LoginName+" Alert:" + myAlert.ListUrl;
}
}
}
}

}

It would be easy to modify this code to clean a single user out of every site if you put an if statement such as the following in the last foreach loop:
if (myAlert.User.LoginName == textbox3.Text)

Comments