0

As the title says, I want to be able to programmatically, using Powershell, add an alert to a SharePoint list when new items are added. Is this possible with SharePoint Online?

I can do it with 2010 but that code doesn't work.

Thanks.

2
  • 1
    Can you please add your code and describe in which way it does not work? Commented Jun 2, 2018 at 5:14
  • Bernd, what happens with alert if $alert.Filter = "2" ? Does it work for you? I guess the alerts property bag will not be written correctly.
    – tomx64
    Commented Feb 5, 2021 at 11:01

1 Answer 1

2

Yes, you can do this with PowerShell. The capability was added to CSOM (client-side object model) for SharePoint Online back in February 2017 as you can read about here: https://dev.office.com/blogs/new-sharepoint-csom-version-released-for-Office-365-february-2017

The quoted article has an example on how to create an alert using CSOM and C#. I have 'translated' the example code to PowerShell and changed the alert type to immediate, which comes in handy for testing.

Get the 'Microsoft.SharePointOnline.CSOM' package from nuget.org, get the two DLLs referenced in the script from it, place them along-side with the PowerShell script and fill-out siteUrl, username, password and listname with your info.

# URL to SharePoint site where you want to add an alert
$siteUrl = ""
# SharePoint user name you want to create the alert for
$username = ""
# SharePoint user password in plain text
$password = ""
$listname = "Documents"

# Load SharePoint client dlls (get them from nuget.org and place them along with this PS1 file)
# For instance Microsoft.SharePointOnline.CSOM or Microsoft.SharePoint2013.CSOM
[System.Reflection.Assembly]::LoadFile("$PSScriptRoot\Microsoft.SharePoint.Client.dll") 
[System.Reflection.Assembly]::LoadFile("$PSScriptRoot\Microsoft.SharePoint.Client.Runtime.dll") 

$context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$securePassword = ConvertTo-SecureString -String $password -AsPlainText -Force
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)

# Get list from SharePoint
$list = $context.Web.Lists.GetByTitle($listname)

# Load needed information from SharePoint Online
$currentUser = $context.Web.CurrentUser
$context.Load($currentUser)
$context.ExecuteQuery()

# Create new alert using CSOM for the user
$alert = New-Object Microsoft.SharePoint.Client.AlertCreationInformation
$alert.List = $context.Web.Lists.GetByTitle("Documents")
$alert.AlertFrequency = [Microsoft.SharePoint.Client.AlertFrequency]::Immediate
$alert.AlertType = [Microsoft.SharePoint.Client.AlertType]::List
$alert.AlwaysNotify = $false
$alert.DeliveryChannels = [Microsoft.SharePoint.Client.AlertDeliveryChannel]::Email
$alert.Status = [Microsoft.SharePoint.Client.AlertStatus]::On
$alert.Title = "New alert created at : " + [System.DateTime]::Now
$alert.User = $currentUser
$alert.EventType = [Microsoft.SharePoint.Client.AlertEventType]::All
$alert.Filter = "0"
# 0 = Anything Changes
# 1 = Someone else changes a document
# 2 = Someone else changes a document created by me
# 3 = Someone else changes a document modified by me

# Add the alert for the user
$newAlertGuid = $currentUser.Alerts.Add($alert)
$currentUser.Update()

# Execute creation request to the SPO
$context.ExecuteQuery();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.