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

357 views Asked by At

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)

1

There are 1 answers

0
Tanddant On

The reason you're seeing this is that RERs are a legacy technology that is tied to a user, and the modern authentication that is being used by PnP does not exist as a user in SharePoint directly.

The only "work around" for now is to use ACS Authentication which is also a legacy technology, the correct thing to to would be to convert to Webhooks, they do have the trade-off of being slower, and not allowing you to run synchronous.

But that's the way MS is pushing.