Developing and troubleshooting custom SharePoint solutions always involves checking, adding, modifying or deleting event receivers. When working on production servers usually we don’t have all the tools that we have in our own VMs. In those cases we can use PowerShell or write inline code in an application page and put it in the LAYOUTS folder in SharePoint Root.
Below are some PowerShell commands to check, add, modify and remove list event receivers.
It goes without saying that you should be very careful when working with event receivers as with everything else in SharePoint. Save the original state with a back up, or at least make sure you know which event receivers are there with all their properties before modifying anything. That way you can get everything back in the original state.
Checking event receivers:
After getting the list in PowerShell you can just run $list.EventReceivers and it will show all the event receivers will all their properties.
Add-PSSnapin Microsoft.SharePoint.PowerShell –erroraction SilentlyContinue
$web = Get-SPWeb -Identity http://....
$list = $web.GetList($web.Url + '/Lists/' + “list name”)
$list.EventReceivers
To save the result in a text file for future reference execute the following command:
$list.EventReceivers | Out-File -filepath "D:\EventReceivers.txt"
Add EventReceiver
To add an event receiver you need to know the full assembly name (you can get that from GAC) and the class name that contains the event receivers
Add-PSSnapin Microsoft.SharePoint.PowerShell –erroraction SilentlyContinue
$web = Get-SPWeb -Identity http://....
$list = $web.GetList($web.Url + "/Lists/" + “list name”)
$type = "ItemAdding" #or any other type, like ItemDeleting, ItemAdded, ItemUpdating ...
$assembly = "YourAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5eff...(here goes assebly's token"
$class = "Your Class"
$list.EventReceivers.Add($type, $assembly, $class)
$web.Dispose()
Modify Event Receiver
For example, to change an after event EventReceiver from asynchronous to synchronous
Add-PSSnapin Microsoft.SharePoint.PowerShell –erroraction SilentlyContinue
$web = Get-SPWeb -Identity http://...
$list = $web.GetList($web.Url + "/Lists/" + “list name”)
$type = "ItemAdded" #or any other type, like ItemDeleting, ItemAdding, ItemUpdating, ...
$numberOfEventReceivers = $list.EventReceivers.Count
if ($numberOfEventReceivers -gt 0)
{
for( $index = $numberOfEventReceivers -1; $index -gt -1; $index–-)
{
$receiver = $list.EventReceivers[$index] ;
$name = $receiver.Name
$typ = $receiver.Type ;
if ($typ -eq $type) #or you can check ($name -eq "event receiver's name") if you have more then one event receivers of the same type
{
$receiver.Synchronization = "Synchronous"
$receiver.Update()
Write-Host "Event receiver " $name " is changed to Synchronous"
}
}
}
else
{
Write-Host " There is no EventReceiver of type " $type " registered for this list "
}
$web.Dispose()
Delete Event Receivers
Run the following to delete a specific event receiver (in this example ItemDeleting event receiver)
Add-PSSnapin Microsoft.SharePoint.PowerShell –erroraction SilentlyContinue
$web = Get-SPWeb -Identity http://...
$list = $web.GetList($web.Url + "/Lists/" + “list name”)
$type = "ItemDeleting" #or any other type, like ItemDeleting, ItemAdding, ItemUpdating, ...
$numberOfEventReceivers = $list.EventReceivers.Count
if ($numberOfEventReceivers -gt 0)
{
for( $index = $numberOfEventReceivers -1; $index -gt -1; $index–-)
{
$receiver = $list.EventReceivers[$index] ;
$name = $receiver.Name
$typ = $receiver.Type ;
if ($typ -eq $type) #or you can check ($name -eq "event receiver's name") if you have more then one event receivers of the same type
{
$receiver.Delete()
Write-Host "Event receiver " $name " is deleted"
}
}
}
else
{
Write-Host " There is no EventReceivers of type " $type " registered for this list "
}
$web.Dispose()