Share via


SPAlertCollection.Add method (SPListItem, SPEventType, SPAlertFrequency, SPAlertDeliveryChannels)

Adds an alert for a list item to the collection.

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

Syntax

'Declaration
Public Function Add ( _
    item As SPListItem, _
    eventType As SPEventType, _
    alertFrequency As SPAlertFrequency, _
    deliveryChannels As SPAlertDeliveryChannels _
) As Guid
'Usage
Dim instance As SPAlertCollection
Dim item As SPListItem
Dim eventType As SPEventType
Dim alertFrequency As SPAlertFrequency
Dim deliveryChannels As SPAlertDeliveryChannels
Dim returnValue As Guid

returnValue = instance.Add(item, eventType, _
    alertFrequency, deliveryChannels)
public Guid Add(
    SPListItem item,
    SPEventType eventType,
    SPAlertFrequency alertFrequency,
    SPAlertDeliveryChannels deliveryChannels
)

Parameters

Return value

Type: System.Guid
The ID of the alert.

Remarks

Use of the Add method sends a confirmation message to each user telling them they have successfully added an alert.

The following code example creates an alert for every user of a site. This alert immediately notifies them whenever a user discusses a specified file in the Shared Documents document library.

Dim web As SPWeb = SPControl.GetContextWeb(Context)
Dim list As SPList = web.Lists("Shared Documents")
Dim item As SPListItem = list.Items(1)
Dim users As SPUserCollection = web.Users
Dim user As SPUser

For Each user In  users

    user.Alerts.Add(item, Microsoft.SharePoint.SPEventType.Discussion, 
        Microsoft.SharePoint.SPAlertFrequency.Immediate, Microsoft.SharePoint.SPAlertDeliveryChannels.Email)

Next user 
SPWeb oWebsite = SPContext.Current.Web;
SPList oList = oWebsite.Lists["Shared Documents"];
SPListItem oItem = oList.Items[1];
SPUserCollection collUsers = oWebsite.Users;
foreach (SPUser oUser in collUsers)
{
   oUser.Alerts.Add(oItem, Microsoft.SharePoint.SPEventType.Discussion, 
      Microsoft.SharePoint.SPAlertFrequency.Immediate, Microsoft.SharePoint.SPAlertDeliveryChannels.Email);
}

See also

Reference

SPAlertCollection class

SPAlertCollection members

Add overload

Microsoft.SharePoint namespace