Get all Event Receivers attached to a list in Sharepoint

We had a strange issue related to re-deploying of an event receiver at work today. I retracted the old solution and added the new one but, at first it threw an error and then it got activated.. So we were good! well just for a while .. as we looked at the logs we saw multiple errors got generated from Eventreciver Manager with assembly missing and other stuff .. at that time we realized that the old event receiver was not completely removed and the event manager is still looking for its assembly to be loaded. So, this lead to an investigation about how to list out and clear all the event receivers attached to a particular list before we re-deploy our solution.

So with the help of microsoft support we were able to put together a console application which will list out all the event receivers attached to a sharepoint list and then on press of enter all of them will be deleted.

Please note you might see some "GUID's" listed out along with your custom event receiver, these might be receivers used by your designer or custom workflows which if deleted will remove the association of the list with the workflow.

here is the code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace EventViewer
{
class Program
{
string siteURL = "http://SPSite";
string listName = "My Custom List";
static void Main(string[] args)
{
Program p = new Program();
p.PrintAllEvRecv();
Console.ReadLine(); -> Press enter to delete all event receivers for My Custom List
p.DeleteAllEventReceivers();

}
//Prints all the Event Receivers attached to a list

public void PrintAllEvRecv()
{


using (SPSite site = new SPSite(siteURL))
{

using (SPWeb web = site.OpenWeb())
{

SPList list = web.Lists[listName];



int i = list.EventReceivers.Count;

SPEventReceiverDefinitionCollection eventReceivers = list.EventReceivers;

foreach (SPEventReceiverDefinition ev in eventReceivers)
{

Console.WriteLine("Type=" + ev.Type.ToString() + "\t Class=" + ev.Class.ToString()+"\t Name: " + ev.Name );
Console.WriteLine(" ");
}

}

}

}
public void DeleteAllEventReceivers()
{
using (SPSite site = new SPSite(siteURL))
{

using (SPWeb web = site.OpenWeb())
{

SPList list = web.Lists[listName];

//Delete Event Receivers



List ersToRemove = new List();

foreach (SPEventReceiverDefinition sprd in list.EventReceivers)
{

//Could add an additional check for the Id or Name of the event receiver

//if(sprd.Id or sprd.Name == ...

ersToRemove.Add(sprd);

}

foreach (SPEventReceiverDefinition sprd in ersToRemove)
{

sprd.Delete();

}
} } }}}

0 comments:

Post a Comment

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer or anyone else. Should you have any questions or concerns please e-mail me at sharepointprogrammingblogger@gmail.com .

Copyright (c) 2010 @ myshaepointwork.blogspot.com. All rights are reserved.Do Not Copy.

@ Learning SharePoint.com