Add-Remove-Get Event Receivers in SharePoint with PowerShell
We usually use C# console application or feature activation code to register event receivers with the target list or library in SharePoint. At times, we may have to handle event receiver associations explicitly. Of course, there are some great tools (E.g. SharePoint Event Receiver Manager) to manage event receivers. However, in a production environment, which didn’t allow me to use any such tools for security reasons, I’m left with the option: PowerShell. Hence, I’m sharing my PowerShell code snippets to manage event receivers in SharePoint using PowerShell.
Get all event receivers attached with a List:
Let’s find in SharePoint 2010 PowerShell to get event receivers associated with a particular list.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Get the web
$Web = Get-SPWeb "https://sharepoint.crescent.com"
#Get the Target List
$List = $Web.Lists["Documents"]
#Retrieve all event receivers associated with the list
$List.EventReceivers | Select Id, Type, Assembly, Class | FL
Find all Event receivers on all sites in a web application:
Similarly, We can retrieve all event receivers associated with various lists and libraries across all site collections using the below PowerShell script.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$WebAppURL = "https://sharepoint.crescent.com"
#Get All Webs
$Webs = Get-SPWebApplication $WebAppURL | Get-SPSite -Limit All | Get-SPWeb
foreach($web in $Webs)
{
#Get Lists with Event receivers excluding System lists
$web.lists | where { ($_.Author.LoginName -ne "SHAREPOINT\system") -and ($_.Hidden -eq $false) -and ($_.IsCatalog -eq $false) -and ($_.IsSiteAssetsLibrary -eq $false) -and ($_.EventReceivers.count -gt 0) } | Select Title, EventReceivers | FL
}
This PowerShell checks list event receiver in SharePoint.
SharePoint PowerShell to Add Event Receiver to List
Usually, we create a C#.net console application to associate an event receiver to a particular list. This time, I tried with PowerShell script: let’s deploy event receiver in SharePoint 2013 using PowerShell.
Let’s add event receiver by creating the definition in PowerShell:
#Get the web
$Web = Get-SPWeb "https://sharepoint.crescent.com"
#Get the Target List
$List = $Web.Lists["Documents"]
#Add new event receiver definition
$def = $list.EventReceivers.Add()
$def.Assembly = "Crescent.DocRestrict, Version=1.0.0.0, Culture=neutral, PublicKeyToken=677b45b1314c252c"
$def.Class = "Crescent.Utilities.DocRestrict.Restrict"
$def.Type = [Microsoft.SharePoint.SPEventReceiverType]::ItemAdded
$def.Name = "ItemAdded Event Receiver";
$def.SequenceNumber = 3000
$def.Synchronization = [Microsoft.SharePoint.SPEventReceiverSynchronization]::Synchronous
$def.Update()
SharePoint Add Event Receiver with PowerShell
Alternatively, in SharePoint to add event receiver to list with PowerShell:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Get the web
$Web = Get-SPWeb "https://sharepoint.crescent.com"
#Get the Target List
$List = $Web.Lists["Documents"]
#Retrieve all event receivers associated with the list
#$List.EventReceivers | Select Id, Type, Assembly, Class | FL
$Assembly = "Crescent.DocRestrict, Version=1.0.0.0, Culture=neutral, PublicKeyToken=677b45b1314c252c"
$Class= "Crescent.Utilities.DocRestrict.Restrict"
Write-Host "Attaching Event Receiver..."
# sharepoint 2010 powershell register event receiver
$list.EventReceivers.Add("ItemAdding", $Assembly , $Class)
How about content types? can add event receiver using PowerShell? why not?
#Get the content type
$ctype = $web.ContentTypes["content type name"]
#Add event receiver
$ctype.EventReceivers.Add("ItemAdding", "Assembly Name", "Class")
SharePoint Delete Event Receiver with PowerShell:
To delete event receiver using PowerShell in SharePoint, here is the code:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Get the web
$Web = Get-SPWeb "https://sharepoint.crescent.com"
#Get the Target List
$List = $Web.Lists["Documents"]
$Assembly = "Crescent.DocRestrict, Version=1.0.0.0, Culture=neutral, PublicKeyToken=677b45b1314c252c"
$EventReceivers = $list.EventReceivers | Where {$_.Assembly -eq $assembly}
if ($EventReceivers) #.Count -gt 0)
{
foreach($Receiver in $EventReceivers)
{
Write-Host "Deleting Event Receiver from " $list.RootFolder.ServerRelativeUrl
$Receiver.Delete()
}
}
SharePoint PowerShell to Remove Event Receiver:
You can also delete a particular event receiver by its ID. Here is an example of remove event receivers from the SharePoint list using PowerShell.
#Get the web
$Web = Get-SPWeb "https://sharepoint.crescent.com"
#Get the Target List
$List = $Web.Lists["Documents"]
#Get all event receivers associated with the list
$List.EventReceivers | Select Id, Type, Assembly, Class | FL
#$EventReceiverID = "625c59a9-72d7-4479-a1e7-35f040e4f9a1"
#Uncomment below line to sharepoint 2010 remove event receiver from list powershell
#$list.EventReceivers[[GUID]$EventReceiverID].delete()
Remove Duplicate Event Receivers:
Duplicate instances of SharePoint event receivers is a known issue. They share a common assembly with different sequence numbers. So they fire twice.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Get the web
$Web = Get-SPWeb "https://sharepoint.crescent.com"
#Get the Target List
$List = $Web.Lists["Documents"]
#Get all event receivers grouped by assembly/class/type
$list.EventReceivers | Group-object assembly, class, type | where { $_.Count -gt 1 }
#If any of the assembly/class/type pairs are same, we have a duplicate!
#Leave the first event receiver at 0th position and delete all other
$list.EventReceivers | Group-object assembly, class, type | where { $_.Count -gt 1 } | foreach { $_.Group[1..50] } | foreach { $_.Delete() }
To Add event receivers with SharePoint list Programmatically with C#, refer: SharePoint add event receiver to list
Hi Salaudeen, very nice tutorial.
How do i execute a powershell skript (.ps1 file) on Event Itemadded?
Kind regards
Axel
That’s not straightforward, I guess! You can create a Microsoft Power Automate and call a PowerShell function from Azure Automation.
Hi Salaudeen. Great site, much appreciated.
We’ve identified 60 non-Microsoft, custom event receiver duplicates.
I’m trying to delete them using your last script.
I’ve listed out the names of the SPWebs and Lists to plug into the script. When I run the script, it completes instantly, and doesn’t seem to delete the duplicate: I ran through these last week, and results (total number of duplicates) are unchanged.
I did a get-variable test, to see if the variables were being passed, it seems like they’re not:
PS C:Windowssystem32> $Web = Get-SPWeb “https://intranet/sites/hr”
PS C:Windowssystem32> get-variable web
Name Value
—- —–
Web
Any ideas? Much thanks.
how can i use in online sharepoint.
please help me
We found an orphan event receiver which is binding to ListTemplateId=”115″. How should we locate it and delete it with PowerShell?
Your Event receiver should be at SPSite.EventReceivers, if your event receiver deployed at Site scope targeting a ListTemplate!