다음을 통해 공유


SPAlertCollection.Delete Method (Guid)

Deletes the alert with the specified GUID from the collection.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
Public Sub Delete ( _
    idAlert As Guid _
)
'Usage
Dim instance As SPAlertCollection
Dim idAlert As Guid

instance.Delete(idAlert)
public void Delete(
    Guid idAlert
)

Parameters

Exceptions

Exception Condition
SPException

The alert does not exist or has just been deleted.

Remarks

This method deletes the specified alert from the database.

Examples

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

Private Sub Button1_Click(sender As Object, e As System.EventArgs)
    Dim siteCollection As SPSite = SPControl.GetContextSite(Context)
    Dim sites As SPWebCollection = siteCollection.AllWebs
    Dim site As SPWeb
    For Each site In sites
        DeleteAlerts(site)
    Next web
End Sub 'Button1_Click


Public Sub DeleteAlerts(site As SPWeb)
    Dim users As SPUserCollection = site.Users
    Dim user As SPUser

    For Each user In  users
        If user.LoginName = TextBox1.Text Then
            Dim alerts As SPAlertCollection = user.Alerts
            Dim i As Integer
            For i = alerts.Count - 1 To 0 Step -1
                Dim guid As System.Guid = alerts(i).ID
                alerts.Delete(guid)
            Next i
        End If
    Next user

    Dim subsite As SPWeb

    For Each subsite In  web.Webs
        DeleteAlerts(subsite)
    Next subsite

End Sub 'DeleteAlerts
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);
            }
        }
    }

Note

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.

See Also

Reference

SPAlertCollection Class

SPAlertCollection Members

Delete Overload

Microsoft.SharePoint Namespace