kwizcom banner advertisement

SharePoint Online: Create Alerts using PowerShell CSOM

Requirement: Create SharePoint Alert using PowerShell

How to Create Alerts in SharePoint Online?
Alerts in SharePoint Online helps to track changes such as addition/changes/delete to the content E.g. Lists and libraries, pages, etc. 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".
    sharepoint online powershell create alert
  • In the new alert creation page, Type the alert title, enter users who will receive notifications,  Select E-mail or SMS for "Delivery Method". Select the condition for this alert (e.g. "Anything changes") and when to send alerts.
    create sharepoint online alert powershell
  • Scroll to the bottom of the page and click "OK" to create an alert for the document library.
Now, Lets create SharePoint online alert using PowerShell!

SharePoint Online: PowerShell to Create Alert
We want to add an alert programmatically using PowerShell to a SharePoint Online list when new items are added.
#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
}
SharePoint Online: Create Alerts using PowerShell CSOM SharePoint Online: Create Alerts using PowerShell CSOM Reviewed by Salaudeen Rajack on October 28, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.