Quantcast
Channel: Gordon Duthie's Blog » Sharepoint 2010
Viewing all articles
Browse latest Browse all 10

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

$
0
0

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



Viewing all articles
Browse latest Browse all 10

Trending Articles