'Add-PnPEventReceiver only works fully when -UseWebLogin parameter is used

I'm using PnP Powershell to add an Event Receiver to a document library. The Event Receiver points to an Azure function. Below is code that I wrote in a local Powershell script that can add an Event Receiver using the Connect-PnPOnline with the -UseWebLogin. This works for a one-off, but not autonomous scripting for "N" amount of site libraries to add an Event Receiver.

The code below has both Connect-PnPOnline versions I've tried (it's written in a format that would work in the confines of an Azure Function, which would be preferred).

Version 1:

  • Event Receiver Added to List: Yes
  • Event Receiver is triggered: No

Version 2:

  • Event Receiver Added to List: Yes
  • Event Receiver is triggered: Yes

Code

#Parameters
$ListName = "MyCustomDocumentLibrary"
$SiteURL = "https://mycustomsite.sharepoint.com/sites/testsite"
$ReceiverName = "MyCustomEventReceiver"
$ReceiverApiUrl = https://....azurewebsites.net/api/..."

#Connection Parameters
$securePassword = ConvertTo-SecureString $env:tenant_pwd -AsPlainText -Force
$credentials = New-Object PSCredential ($env:tenant_user, $securePassword)

# Version 1
#Connect to SharePoint Online (with credentials)
#Result: Event Receiver Added to List; ItemAdded does not trigger this Event Receiver
Connect-PnPOnline -Url $SiteURL -Credential $Credentials
    Add-PnPEventReceiver -List $ListName -Name $ReceiverName -Url $ReceiverApiUrl -EventReceiverType ItemAdded -Synchronization Synchronous -SequenceNumber 33500 -Force | Out-Null
Disconnect-PnPOnline

# Version 2
#Connect to SharePoint Online (with Web Login)
#Result: Event Receiver Added to List; ItemAdded does trigger this Event Receiver
Connect-PnPOnline $SiteURL -UseWebLogin
    Add-PnPEventReceiver -List $ListName -Name $ReceiverName -Url $ReceiverApiUrl -EventReceiverType ItemAdded -Synchronization Synchronous -SequenceNumber 33500 -Force | Out-Null
Disconnect-PnPOnline

Late in 2021, some developers were discussing this issue over in this project's Github, but didn't seem to come to a resolution other than using the "-UseWebLogin" as a stop-gap measure. (https://github.com/pnp/powershell/issues/464)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source