Checking if SharePoint Event Receivers are attached to a list using PowerShell.

Posted: 15th July 2011 in Powershell, Sharepoint 2010

After deploying several event receivers to a variety of lists I found that one of the event receivers did not seem to be working, but instead was firing a old version of the DLL.

So, to check that it was indeed attached, I ran the following PowerShell command.

$spWeb = Get-SPWeb –Identity <SharePointWebUrl> 
$spList = $spWeb.Lists["List Display Name"]  
$spList.EventReceivers | Select Name, Assembly, Type

After running this I discovered that one of the event receivers had been attached to the list twice! I’m not entirely sure why or how this happened, but my problem was resolved by deleting the duplicate receivers and reattaching it once.

The script is used to delete was:

$spWeb = Get-SPWeb –Identity <SharePointWebUrl> 
$spList = $spWeb.Lists["List Display Name"] 
$eventsCount = $spList.EventReceivers.Count
$assembly = "Company.SharePoint.Client.EventReceivers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=570c484d87a3aa61" 
$class = "Company.SharePoint.Client.EventReceivers.ProvisionServiceAreaSecurityGroups.ProvisionServiceAreaSecurityGroups" $name = "ProvisionServiceAreaSecurityGroupsItemUpdating" 
$type = 2 
for ($i = 0; $i -lt $eventsCount; $i+=1)
{ 
     if ($spList.EventReceivers[$i].Assembly -eq $assembly –and
         $spList.EventReceivers[$i].Class -eq $class –and
         $spList.EventReceivers[$i].Type -eq $type –and
         $spList.EventReceivers[$i].Name -eq $Name)
         { 
             $spList.EventReceivers[$i].Delete()
         } 
}

$spList.Update()

And to add it was:

$spWeb = Get-SPWeb –Identity <SharePointWebUrl> 
$spList = $spWeb.Lists["List Display Name"] 
$spEventReceiver = $spList.EventReceivers.Add() 
$spEventReceiver.Assembly = "Company.SharePoint.Client.EventReceivers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=570c484d87a3aa61" 
$spEventReceiver.Class = "Company.SharePoint.Client.EventReceivers.ProvisionServiceAreaSecurityGroups.ProvisionServiceAreaSecurityGroups" $spEventReceiver.Name = "ProvisionServiceAreaSecurityGroupsItemUpdating" 
$spEventReceiver.Type = 2 
$spEventReceiver.SequenceNumber = 1000 
$spEventReceiver.Synchronization = 0 
$spEventReceiver.Update()

 

That’s it Winking smile

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s