SPAlertCollection.Delete method (Guid)

Deletes the alert with the specified GUID from the collection.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

public void Delete(
	Guid idAlert
)

Parameters

idAlert
Type: System.Guid

The alert to delete.

ExceptionCondition
SPException

The alert does not exist or has just been deleted.

This method deletes the specified alert from the database.

The following code example iterates through all the Web sites in the current site collection and deletes the alerts of a specified user.

private void Button1_Click(object sender, System.EventArgs e)
{
    SPSite oSiteCollection = SPContext.Current.Site;
    SPWebCollection collWebsites = oSiteCollection.AllWebs;

    foreach (SPWeb oWebsite in collWebsites)
    {
        DeleteAlerts(oWebsite);
        oWebsite.Dispose(); 
   }
}

public void DeleteAlerts(SPWeb oWebsite)
{
    SPUserCollection collUsers = oWebsite.Users;
    foreach (SPUser oUser in collUsers)
    {
        if (oUser.LoginName == TextBox1.Text)
        {
            SPAlertCollection collAlerts = oUser.Alerts;

            for (int i = collAlerts.Count - 1; i > -1; i--)
            {
                System.Guid guid = collAlerts[i].ID;
                collAlerts.Delete(guid);
            }
        }
    }
NoteNote

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects.

Show: