Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pkskelly/40f3179afd4c1c4542c3 to your computer and use it in GitHub Desktop.
Save pkskelly/40f3179afd4c1c4542c3 to your computer and use it in GitHub Desktop.
. .\TopOfScript_PSCSOM.ps1
function listEventReceivers([Microsoft.SharePoint.Client.ClientContext]$context, [string]$listName)
{
Write-Host "Attempting to iterate event receivers on list '$listName'"
$list = $context.Web.Lists.GetByTitle($listName);
$context.Load($list)
$eventReceivers = $list.EventReceivers
$context.Load($eventReceivers)
$context.ExecuteQuery()
Write-Host "Found" $eventReceivers.Count "receiver(s) on this list"
foreach ($er in $eventReceivers)
{
Write-Host ("Found ER: " + $er.ReceiverName)
Write-Host (" ReceiverClass: " + $er.ReceiverClass)
Write-Host (" ReceiverAssembly: " + $er.ReceiverAssembly)
Write-Host (" EventType: " + $er.EventType)
Write-Host (" ReceiverUrl: " + $er.ReceiverUrl)
Write-Host (" Synchronization: " + $er.Synchronization)
}
}
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: $_"
}
}
function deleteListItemRemoteEventReceiver([Microsoft.SharePoint.Client.ClientContext]$context, [string]$listName, [string]$receiverName)
{
Write-Host "Attempting to delete RER with name '$receiverName' on list '$listName'"
$list = $context.Web.Lists.GetByTitle($listName)
$context.Load($list)
$eventReceivers = $list.EventReceivers
$context.Load($eventReceivers)
$context.ExecuteQuery()
$specifiedEr = $null
try
{
foreach ($er in $eventReceivers)
{
if ($er.ReceiverName -eq $receiverName)
{
$specifiedEr = $er
}
}
if ($specifiedEr -ne $null -and !$specifiedEr.ServerObjectIsNull.Value)
{
$specifiedEr.DeleteObject()
$context.ExecuteQuery()
Write-Host "Receiver deleted successfully"
}
else
{
Write-Error "Failed to find receiver with name '$receiverName'"
}
}
catch
{
Write-Error "Failed to delete 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"
# add
addListItemRemoteEventReceiver $clientContext $listName $receiverEventType $receiverSynchronization $receiverName $receiverUrl $receiverAssemblyName $receiverClassName
# delete
deleteListItemRemoteEventReceiver $clientContext $listName $receiverName
# list
listEventReceivers $clientContext $listName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment