During preupgradecheck, I found many orphaned event handlers listed out. This need to be deleted from almost every site. I googled a lot but not able to find any solution to delete this using code. So I decided to create my own solution.
My Solution is Loop over all web applications, then loop over all lists inside all webs. Look for event receivers attached with list. And then try to look for the assembly associated with event receiver is valid or not. If assembly is invalid delete the event receiver.
Download working project EventReceiverHelper.
Check my code below:
//get local farm
SPFarm localFarm = SPFarm.Local;
//loop Services
foreach (SPService spService in localFarm.Services)
{
//if service is WebService
if (spService is SPWebService)
{
SPWebService webService = (SPWebService)spService;
//loop over web applications
foreach (SPWebApplication webApp in webService.WebApplications)
{
//Loop Site Collections
foreach (SPSite site in webApp.Sites)
{
//Loop Sites
foreach (SPWeb web in site.AllWebs)
{
try
{
//Loop Lists
foreach (SPList list in web.Lists)
{
SPEventReceiverDefinitionCollection spEDC = list.EventReceivers;
if (spEDC.Count > 0)
{
List<Guid> orphandIDs = new List<Guid>();
for (int i = 0; i < spEDC.Count; i++)
{
SPEventReceiverDefinition spED = spEDC[i];
string assembly = spED.Assembly;
string className = spED.Class;
System.Reflection.Assembly eventReceiverAssembly = System.Reflection.Assembly.LoadWithPartialName(assembly);
if (eventReceiverAssembly == null)
orphandIDs.Add(spEDC[i].Id);
}
for (int i = orphandIDs.Count - 1; i >= 0; i--)
{
list.EventReceivers[i].Delete();
}
}
}
}
catch (Exception ex)
{
}
web.Dispose();
}
site.Dispose();
}
}
}
}
}
My Solution is Loop over all web applications, then loop over all lists inside all webs. Look for event receivers attached with list. And then try to look for the assembly associated with event receiver is valid or not. If assembly is invalid delete the event receiver.
Download working project EventReceiverHelper.
Check my code below:
//get local farm
SPFarm localFarm = SPFarm.Local;
//loop Services
foreach (SPService spService in localFarm.Services)
{
//if service is WebService
if (spService is SPWebService)
{
SPWebService webService = (SPWebService)spService;
//loop over web applications
foreach (SPWebApplication webApp in webService.WebApplications)
{
//Loop Site Collections
foreach (SPSite site in webApp.Sites)
{
//Loop Sites
foreach (SPWeb web in site.AllWebs)
{
try
{
//Loop Lists
foreach (SPList list in web.Lists)
{
SPEventReceiverDefinitionCollection spEDC = list.EventReceivers;
if (spEDC.Count > 0)
{
List<Guid> orphandIDs = new List<Guid>();
for (int i = 0; i < spEDC.Count; i++)
{
SPEventReceiverDefinition spED = spEDC[i];
string assembly = spED.Assembly;
string className = spED.Class;
System.Reflection.Assembly eventReceiverAssembly = System.Reflection.Assembly.LoadWithPartialName(assembly);
if (eventReceiverAssembly == null)
orphandIDs.Add(spEDC[i].Id);
}
for (int i = orphandIDs.Count - 1; i >= 0; i--)
{
list.EventReceivers[i].Delete();
}
}
}
}
catch (Exception ex)
{
}
web.Dispose();
}
site.Dispose();
}
}
}
}
}