Forked from chrisobriensp/ManageRemoteEventReceivers.ps1
Last active
August 29, 2015 14:16
-
-
Save pkskelly/40f3179afd4c1c4542c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
. .\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