SharePoint Online: Get All Alerts from a Site Collection using PowerShell

Requirement: View all alerts in SharePoint Online site collection.

How to Get all alerts of a user in SharePoint Online?
Alerts are scoped at web level in SharePoint. So, to get alerts of a user in a specific site, follow these steps:
  • Navigate to the site >> Go to Site Settings page
  • Click on "User alerts" link under "Site Administration"
  • Select the user from the drop down and click on "Update" button to view all alerts.
    sharepoint online powershell get alerts
  • This gets you all alerts of the selected user on the current site .
Now the question is: How to view all alerts of a SharePoint Online user for a entire site collection? PowerShell!

PowerShell to Fetch All Alerts from SharePoint Online:
Here is the SharePoint Online PowerShell to get alerts from a site collection.
#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"
   
Function Get-SPOWebAlerts($SiteURL)
{
    Try {
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Cred
        
        Write-host -f Yellow "Getting Alerts in the site" $SiteURL
        #Get All Alerts from the Web
        $Web = $Ctx.Web
        $WebAlerts = $Web.Alerts
        $Ctx.Load($Web)
        $Ctx.Load($web.Webs)
        $Ctx.Load($WebAlerts)
        $Ctx.ExecuteQuery()

        If($WebAlerts.count -gt 0) { Write-host -f Green "Found $($WebAlerts.Count) Alerts!"}

        $AlertCollection = @()
        #Loop through each alert of the web and get alert details
        ForEach($Alert in $webAlerts)
        {
            #Get Alert list and User
            $Ctx.Load($Alert.User)
            $Ctx.Load($Alert.List)
            $Ctx.ExecuteQuery()

            $AlertData = New-Object PSObject
            $AlertData | Add-Member NoteProperty SiteName($Web.Title)
            $AlertData | Add-Member NoteProperty SiteURL($Web.URL)
            $AlertData | Add-Member NoteProperty AlertTitle($Alert.Title)
            $AlertData | Add-Member NoteProperty AlertUser($Alert.User.Title)
            $AlertData | Add-Member NoteProperty AlertList($Alert.List.Title)
            $AlertData | Add-Member NoteProperty AlertFrequency($Alert.AlertFrequency)
            $AlertData | Add-Member NoteProperty AlertType($Alert.AlertType)
            $AlertData | Add-Member NoteProperty AlertEvent($Alert.EventType)
                      
            #Add the result to an Array
            $AlertCollection += $AlertData
        }
        #Export Alert Details to CSV file
        $AlertCollection | Export-CSV $ReportOutput -NoTypeInformation -Append

        #Iterate through each subsite of the current web and call the function recursively
        foreach ($Subweb in $web.Webs)
        {
            #Call the function recursively to process all subsites underneath the current web
            Get-SPOWebAlerts($Subweb.url)
        }
    }
    Catch {
        write-host -f Red "Error Getting Alerts!" $_.Exception.Message
    }
}

#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/"
$ReportOutput="C:\Temp\AlertsRpt.csv"

#Delete the Output Report, if exists
if (Test-Path $ReportOutput) { Remove-Item $ReportOutput }

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

#Call the function
Get-SPOWebAlerts $SiteURL
This PowerShell script gets alerts created in site collection and generates a CSV file with all alerts in the provided site collection.

PnP PowerShell to Get All Alerts of a User in a Site 
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$UserID = "i:0#.f|membership|[email protected]"

#Connect to SharePoint Online site
Connect-PnPOnline $SiteURL -Credential (Get-Credential)

#Get the User
$User = Get-PnPUser -Identity $UserID

#Get All Alerts of the User in the web
$Alerts = Get-PnPAlert -User $User

[email protected]()
ForEach($Alert in $Alerts)
{
    #Get the List and List URL of the Alert
    $AlertList =  Get-PnPProperty -ClientObject $Alert -Property List
    $ListURL =  Get-PnPProperty -ClientObject $AlertList -Property DefaultViewURL

    #Fetch Alert Data
    $AlertData += New-Object PSObject -Property @{
        Title = $Alert.Title
        List = $AlertList.Title
        URL = $ListURL
        Frequency = $Alert.AlertFrequency
        AlertType = $Alert.AlertType
        EventType = $Alert.EventType
    }
}
$AlertData 

2 comments:

  1. Hi, a bit off-topic maybe but one question:

    I have set up change alerts for my SP Online list. They work - as long as the changes are made through the web interface. But if I change items using PnP (Set-PnPListItem) I don´t get any alert. Is there any way to change that, so is there a way to let PnP initiated changed trigger the regular change alerts?

    ReplyDelete
  2. PS: I just found that the changes made through Set-PnpListItem don´t even change the last modified timestamp. So the change is actually done as kind of "hidden" change or something. Bug or intention? Just found the GitHub issues list. Will try to look for it there.

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.