MOSS MVP

I've moved my blog to http://blog.falchionconsulting.com!. Please update your links. This blog is no longer in use--you can find all posts and comments at my new blog; I will no longer be posting to this site and comments have been disabled.

Thursday, March 5, 2009

Deleting Orphaned Event Receivers using PowerShell

While looking through the event logs at a client of mine the other day I came across an odd error that was occurring regularly.  Apparently they had developed a Feature which contained an Event Receiver which was programmatically bound to a series of lists.  As time went on their requirements changed so they deleted the event receiver class.  Problem was that the binding to the lists still exists so every time an item was updated an error would be dumped to the event log about a missing type in the specified assembly.

Fortunately it’s pretty easy to fix this using some simple PowerShell:

$site = get-spsite -url http://portal
foreach ($web in $site.AllWebs) {
    $lists = $web.Lists | where {$_.EventReceivers.Count -gt 0}
    foreach ($list in $lists) {
        $evts = $list.EventReceivers | where {$_.Class -eq MyCompany.SharePoint.MyFeature.EventReceivers.MyEventReceiver"}
        if ($evts.Count -gt 0) {
            foreach ($evt in $evts) {
                Write-Host("Deleting..." + $list.RootFolder.ServerRelativeUrl + ", " + $evt.Type)
                $evt.Delete()
            }
        }
    }
}

The only thing that threw me off when I was putting this little snippet together was the fact that I couldn’t work with the collections directly due to errors about the collection being modified during the enumeration (so a “for” loop rather than a “foreach” loop would have worked just as easily as doing filtering as I am above).

The code above is great for when you know the class name of a specific event receiver that needs to be pulled.  If you don’t know the name then you’ll have to add in some reflection to look for the assembly and type – just be careful as the assembly may or may not be in the GAC.

No comments: