Partilhar via


SPAlertCollection Class

Represents a collection of SPAlert objects.

Inheritance Hierarchy

System.Object
  Microsoft.SharePoint.Administration.SPAutoSerializingObject
    Microsoft.SharePoint.SPBaseCollection
      Microsoft.SharePoint.SPAlertCollection

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

Syntax

'Declaration
<SubsetCallableTypeAttribute> _
Public Class SPAlertCollection _
    Inherits SPBaseCollection
'Usage
Dim instance As SPAlertCollection
[SubsetCallableTypeAttribute]
public class SPAlertCollection : SPBaseCollection

Remarks

The SPAlertCollection object is available as a property of either SPWeb or SPUser.

Use the Alerts property of the SPUser or SPWeb class to return the collection of alerts for the Web site or user. To create an alert, use one of the Add methods of the SPAlertCollection class.

Use an indexer to return a single field from the collection. For example, assuming the collection is assigned to a variable named collAlerts, use collAlerts[index] in C#, or collAlerts(index) in Visual Basic, where index is either the index number of the alert in the collection or the display name of the alert.

Examples

The following code example iterates through all the alerts within a site collection to display the title of each site, the display name of the list and item to which each alert applies, and the name of the user who receives each alert.

The example assumes the existence of an .aspx page that contains a label control.

Dim siteCollection As SPSite = SPControl.GetContextSite(Context)
Dim sites As SPWebCollection = siteCollection.AllWebs
Dim site As SPWeb

For Each site In  sites

    Dim alerts As SPAlertCollection = site.Alerts
    Dim alert As SPAlert

    For Each alert In  alerts

        Label1.Text += SPEncode.HtmlEncode(site.Title) & " :: " _
            & SPEncode.HtmlEncode(alert.Title) & " :: " _
            & alert.User.LoginName & "<BR>"

    Next alert

Next site
SPSite oSiteCollection = SPContext.Current.Site;
SPWebCollection collWebsites = oSite.AllWebs;
foreach (SPWeb oWebsite in collWebsites)
{
    SPAlertCollection collAlerts = oWebsite.Alerts;

    foreach (SPAlert oAlert in collAlerts)
    {
        Label1.Text = SPEncode.HtmlEncode(oWebsite.Title) + " :: " +
            SPEncode.HtmlEncode(oAlert.Title) + " :: " +
            oAlert.User.LoginName + "<BR>";
        writer.Write(strLabelText);
    }
    oWebsite.Dispose();
}

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.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

SPAlertCollection Members

Microsoft.SharePoint Namespace