Skip to content

Instantly share code, notes, and snippets.

@chrisobriensp
Last active June 13, 2023 15:17
Show Gist options
  • Save chrisobriensp/8574254 to your computer and use it in GitHub Desktop.
Save chrisobriensp/8574254 to your computer and use it in GitHub Desktop.
PowerShell/CSOM to add a Remote Event Receiver to a SharePoint list
. .\TopOfScript_PSCSOM.ps1
function addListItemRemoteEventReceiver([Microsoft.SharePoint.Client.ClientContext]$context, [string]$listName,
[Microsoft.SharePoint.Client.EventReceiverType]$eventType, [Microsoft.SharePoint.Client.EventReceiverSynchronization]$synchronization,
[string]$receiverName, [string]$receiverUrl, [string]$receiverAssemblyName, [string]$receiverClassName)
{
$list = $context.Web.Lists.GetByTitle($listName);
$context.Load($list)
$eventReceivers = $list.EventReceivers
$context.Load($eventReceivers)
$context.ExecuteQuery()
$newRER = New-Object Microsoft.SharePoint.Client.EventReceiverDefinitionCreationInformation
$newRER.EventType = $eventType
$newRER.ReceiverName = $receiverName
$newRER.ReceiverClass = $receiverClassName
$newRER.ReceiverAssembly = $receiverAssemblyName
$newRER.ReceiverUrl = $receiverUrl
$newRER.Synchronization = $synchronization
$newRER.SequenceNumber = 10
try
{
$list.EventReceivers.Add($newRER)
$list.Update()
$context.ExecuteQuery()
Write-Host "Receiver added successfully"
}
catch
{
Write-Error "Failed to add receiver - error: $_"
}
}
# TODO: replace these parameters with your own!
$listName = "Items"
$receiverName = "COB_ListItemAdding"
$receiverClassName = "RER_ListItemAdded"
$receiverEventType = [Microsoft.SharePoint.Client.EventReceiverType]::ItemAdding
$receiverSynchronization = [Microsoft.SharePoint.Client.EventReceiverSynchronization]::Synchronous
$receiverUrl = "https://cobspdev.azurewebsites.net/Services/MyService_ListItemAdded.svc"
$receiverAssemblyName = "COB-SP-RemoteCodeWeb, Version=1.0.0.0, Culture=neutral, PublicKeyToken=foo"
# now call function..
addListItemRemoteEventReceiver $clientContext $listName $receiverEventType $receiverSynchronization $receiverName $receiverUrl $receiverAssemblyName $receiverClassName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment