SharePoint Online: Create Alerts using PowerShell
Requirement: Create a SharePoint Alert using PowerShell.
How to Create Alerts in SharePoint Online?
Alerts in SharePoint Online help to track changes such as addition/changes/deletes to the content, E.g., Lists and libraries, pages, etc. You can specify the conditions under which you want to receive alerts and the frequency with which you want to receive them. To create an Alert for a SharePoint Online document library, follow these steps:
- Go to your SharePoint Online site, Navigate to the document library for which you want to create an alert
- From the Toolbar, click on the ellipses and select “Alert Me”.
- In the new alert creation page, type the alert title, enter users who will receive notifications, and Select E-mail or SMS for “Delivery Method”. Select the condition for this alert (e.g., “Anything changes”) and when to send alerts.
- Scroll to the bottom of the page and click “OK” to create an alert for the document library.
Now, let’s create a SharePoint Online alert using PowerShell!
SharePoint Online: CSOM PowerShell to Create Alert
When new items are added, we want to add an alert programmatically using PowerShell to a SharePoint Online list.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com"
$ListName="Documents"
$UserID="Salaudeen@crescent.com"
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred
#Get the List and User Objects
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$User = $Ctx.Web.EnsureUser($UserID)
$Ctx.Load($List)
$Ctx.Load($User)
$Ctx.ExecuteQuery()
#Create new alert Object and Set its Properties
$Alert = New-Object Microsoft.SharePoint.Client.AlertCreationInformation
$Alert.List = $List
$Alert.User = $User
$Alert.Title = "New Documents in Project Docs - Alert"
$Alert.AlertFrequency = [Microsoft.SharePoint.Client.AlertFrequency]::Immediate
$Alert.AlertType = [Microsoft.SharePoint.Client.AlertType]::List
$Alert.DeliveryChannels = [Microsoft.SharePoint.Client.AlertDeliveryChannel]::Email
$Alert.Status = [Microsoft.SharePoint.Client.AlertStatus]::On
$Alert.EventType = [Microsoft.SharePoint.Client.AlertEventType]::AddObject #Or All
$Alert.Filter = "0" #Anything Changes - Other values: 1, 2, 3
# Add the alert for the user
$AlertGuid = $User.Alerts.Add($Alert)
$User.Update()
$Ctx.ExecuteQuery()
Write-host -f Green "New Alert Has been Created!"
}
Catch {
write-host -f Red "Error Creating Alert!" $_.Exception.Message
}
PnP PowerShell to Create New Alert in SharePoint Online
If you want to create new alerts in SharePoint Online without using the UI, PowerShell is the answer! Let me show you how to use the PnP PowerShell cmdlet Add-PnPAlert to create new alerts in SharePoint Online.
Add-PnPAlert -Url <List or Library URL> -User <User or Group Email> -DeliveryMethod <Delivery method> -Frequency <Frequency> -ChangeType <Change Type>
Here is the list of parameters it takes:
<List or Library URL>
: The URL of the list or library for which you want to create an alert.<User or Group Email>
: The email address of the user or group for whom you want to create the alert.<Delivery Method>
: The type of alert you want to create, such as “Email” or “SMS”.<Frequency>
: The frequency at which you want to receive alerts, such as “Immediate” or “Weekly”.<Change Type>
: The type of changes for which you want to receive alerts, such as “All” or “New”.
For example, to create an alert for “Documents” on the site URL “https://crescent.sharepoint.com/sites/Branding” and send daily email alerts to the user with the email address “Steve@Crescent.com” for all changes, you can use the following command:
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Branding"
$UserId= "i:0#.f|membership|Steve@crescent.com"
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Create an Alert
Add-PnPAlert -Title "Documents Alert" -List "Documents" -User $UserId -Frequency Daily -ChangeType All
Write-host "New Alert has been Created for the given user!" -f Green
}
Catch {
Write-host -f Red "Error:" $_.Exception.Message
}
how can i create alert for page not for list using pnp.
Hi Thanks, for sharing this script, i have observed in this script $Alert.Filter Property values are not updated in target. Could you share who we can update the $Alert.Filter Property value.
hi the script is very good but iam unable to update the $Alert.Filter value
yes iam also facing the same problem please let me know if anyone got how to update that feature
Did anyone get this working?