Second Life of a Hungarian SharePoint Geek

July 8, 2013

Finding and removing orphaned event receivers using PowerShell

Filed under: Event receivers, PowerShell, SP 2010 — Tags: , , — Peter Holpar @ 23:56

Recently I found a lot of errors in the Windows / SharePoint logs on one of our servers, stating, that SharePoint event manager cannot load the registered classes to handle specific events.

SharePoint log:

06/28/2013 10:35:06.94                OWSTIMER.EXE (0x2AA0)                           0x2DFC SharePoint Foundation                General                                       6644      Critical  Event manager error: Could not load type ‘My.Namespace.EventReceiverClass’ from assembly ‘My.Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f277a09de9ab6579’.          530f203f-2f99-4b24-9ba8-c59adf5039b6

Windows log:

Event manager error: Could not load type ‘My.Namespace.EventReceiverClass’ from assembly ‘My.Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f277a09de9ab6579’.

image

I wrote a few PowerShell snippets to help to find the orphaned event receiver registrations referring to the type mentioned in the error message.

First, searching event receivers on a specific list and all lists of the site:

# List event receivers of a specific list
$list = $web.Lists["yourlist"]
$list.EventReceivers | % { $_.Assembly, $_.Class, $_.Type }

# List event receivers of all lists of the current site, where the class name matches the type reported in the event log
$web.Lists | % {$_.EventReceivers} | ? { $_.Class -eq ‘My.Namespace.EventReceiverClass’ } | % { $_.Assembly, $_.Class, $_.Type }

Next, searching event receivers registered for a specific content type and all content types of the site:

# List event receivers of a specific content type
$ct = $site.RootWeb.ContentTypes["yourcontenttype"]
$ct.EventReceivers | % { $_.Assembly, $_.Class, $_.Type }

# List event receivers of all content types of the current site, where the class name matches the type reported in the event log
$site.RootWeb.ContentTypes | % {$_.EventReceivers} | ? { $_.Class -eq ‘My.Namespace.EventReceiverClass’ } | % { $_.Assembly, $_.Class, $_.Type }

Finally, search site event receivers:

# List site event receivers, where the class name matches (using wildcards) the type reported in the event log
$site.EventReceivers | ? { $_.Class -like ‘*EventReceiverClass*’ } | % { $_.Assembly, $_.Class, $_.Type }

The problematic event receivers were found among the site event receivers, so I wrote a script to remove them. To avoid the infamous “Collection was modified; enumeration operation may not execute.” error, we should remove the event receivers in two steps. First, the IDs of the event receivers are determined, then in the second step the event receivers are deleted by ID:

# Remove site event receivers, where the class name matches (using wildcards) the type reported in the event log
$evenReceiverIds = $site.EventReceivers | ? { $_.Class -like ‘*EventReceiverClass*’ } | % { $_.ID }
$evenReceiverIds | % {
  $er = $site.EventReceivers[$_]
  Write-Host Deleting  $er.Assembly, $er.Class, $er.Type
  $er.Delete()
  Write-Host Deleted
}

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

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

Blog at WordPress.com.

%d bloggers like this: