/// <summary>
/// This function remove duplicate events that were created when
/// saving a list with an event receiver as a template and creating
/// instances from the template
/// </summary>
/// <param name="list">The list to check for duplicate events</param>
/// <param name="className">The class which contains the receiver function</param>
/// <param name="eventType">The event type (if more than one event reciver in class)</param>
/// <returns>True if duplicate events were found, False otherwise</returns>
private bool RemoveDuplicateEvents(SPList list, string className, SPEventReceiverType eventType)
{
//List of pointers to the duplicate events
List<int> evnetReciverPointers = new List<int>();
bool rc = false;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
if (list.EventReceivers.Count < 2)
{
rc = false;
return;
}
//Find events from the same type and classs
for (int i = 0; i < list.EventReceivers.Count; i++)
{
if ((list.EventReceivers[i].Class == className) && (list.EventReceivers[i].Type == eventType))
{
evnetReciverPointers.Add(i);
}
}
//We delete one at a time
if (evnetReciverPointers.Count > 1)
{
list.EventReceivers[evnetReciverPointers[1]].Delete();
list.Update();
rc = true;
}
});
}
catch (Exception ex)
{
//Do some error handling
}
return rc;
}