This MSDN article has a nice section on adding and removing event receivers. However, after trying their code below to delete an event receiver, it will fail if you try to remove multiple event receivers.
you will get this error:
Collection was modified; enumeration operation may not execute.
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPSite sitecollection = properties.Feature.Parent as SPSite;
SPWeb site = sitecollection.AllWebs[“Docs”];
SPList list = site.Lists[“MyList”];
foreach (SPEventReceiverDefinition rd in list.EventReceivers)
{
if (rd.Name == “My Event Receiver”)
rd.Delete();
}
To remedy this I specifically get the GUID’s of each event receiver and then delete them declaratively..
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPSite sitecollection = properties.Feature.Parent as SPSite;
SPWeb site = sitecollection.AllWebs[“Site Name”];
SPList list = site.Lists [“List Name”];
Guid Event1_Added = Guid.Empty;
Guid Event1_Updated = Guid.Empty;
foreach (SPEventReceiverDefinition rd in list.EventReceivers)
{
if (rd.Name == “Event1_Added”)
{
Event1_Added = rd.Id;
}//
else if(rd.Name == “Event1_Updated”)
{
Event1_Updated = rd.Id;
}//
}
if (Event1_Added != Guid.Empty)
{
list.EventReceivers[Event1_Added].Delete();
}//
if (Event1_Updated != Guid.Empty)
{
list.EventReceivers[Event1_Updated].Delete();
}//
}
A nice way to see which event receivers are still on a list is to download SPManager from codeplex:
By using this I found I was only deleting one of two event receivers every time I deactivated the feature.
Thanks, I’ve had this issue as well, I usually have multiple event receivers tied to a custom list. Your response gave me an idea to capture the ID’s of all in a generic list and enumerate through that to delete.
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPWeb site = null;
try
{
if (properties.Feature.Parent is SPWeb)
{
site = (SPWeb)properties.Feature.Parent;
}
else if (properties.Feature.Parent is SPSite)
{
site = ((SPSite)properties.Feature.Parent).RootWeb;
}
else
{
throw new Exception(“Unable to retrieve SPWeb – this feature is not Site or Web-scoped.”);
}
SPList lst = null;
try
{
lst = site.Lists[“Incidents”];
}
catch (Exception ex)
{
throw new Exception(“error getting list ‘Incidents, error: ” + ex.Message);
}
string asmName = “TTSEventReceiver, Version=1.0.0.0, Culture=neutral, PublicKeyToken=204bd89c2dcf2b07”;
SPEventReceiverDefinitionCollection eventReceivers =
lst.EventReceivers;
List erGuids = new List();
try
{
foreach (SPEventReceiverDefinition er in eventReceivers)
{
//found an event receiver for assembly, “mark” for deletion by adding to list
if (er.Assembly == asmName)
{
erGuids.Add(er.Id);
}
}
}
catch (Exception ex)
{
UTTS.Common.WriteErrorTable(site, “UTTS Ticket handling delete event receiver “, ex.Message, “ERROR”, “”);
}
try
{
foreach (Guid erID in erGuids)
{
lst.EventReceivers[erID].Delete();
}
}
catch (Exception ex)
{
UTTS.Common.WriteErrorTable(site, “UTTS Ticket handling delete event receiver “, ex.Message, “ERROR”, erIDdoc.ToString());
}
}
catch (Exception ex)
{
UTTS.Common.WriteErrorTable(site, “main: UTTS Ticket handling delete event receiver “, ex.Message, “ERROR”, “”);
}
}