Wednesday, February 12, 2014

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 in codeplex (E.g. http://speventreceiverman.codeplex.com/) 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:
Lets 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 "http://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 
 
Lets 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 collection using the below PowerShell script.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$WebAppURL = "http://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
 } 

SharePoint 2010 powershell add event receiver
Usually, we create a C#.net console application to associate a event receiver to a particular list. This time, I tried with PowerShell script:

Lets add event receiver by creating the definition in PowerShell:
#Get the web
$Web = Get-SPWeb "http://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() 

Alternatively, in SharePoint 2010 to add event receiver to list with PowerShell:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

 #Get the web
 $Web = Get-SPWeb "http://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")

Remove Event Receiver:
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 "http://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 remove event receiver:
You can also delete a particular event receiver by its ID.
 #Get the web
 $Web = Get-SPWeb "http://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 "http://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 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, refer: sharepoint powershell add event receiver to list



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...