private void DeleteEventReceiverHook(Guid guid, ISecurableObject eventContiner) { SPEventReceiverDefinitionCollection definitions = null; if (typeof(SPWeb).IsAssignableFrom(eventContiner.GetType())) { SPWeb web = (SPWeb)eventContiner; definitions = web.EventReceivers; } else if (typeof(SPList).IsAssignableFrom(eventContiner.GetType())) { SPList list = (SPList)eventContiner; definitions = list.EventReceivers; } else { MessageBox.Show(this, "The current type is not supported.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } if (definitions != null) { foreach (SPEventReceiverDefinition definition in definitions) { if (guid == definition.Id) { string type = definition.Type.ToString(); string name = definition.Class; definition.Delete(); MessageBox.Show(this, string.Format("The event hook [{0}] to class [{1}] was deleted.", type, name), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); break; } } } }
public static void ProcessEventHookAction(Form parent, ISecurableObject obj, string typeName, Assembly selectedAssembly, Type selectedType, bool useNextSequenceNumber) { Type selectedObjectType = obj.GetType(); SPEventReceiverType eventType = (SPEventReceiverType)Enum.Parse(typeof(SPEventReceiverType), typeName); int maxSequence = 9999; SPEventReceiverDefinitionCollection definitions = null; string parentName = string.Empty; if (typeof(SPWeb).IsAssignableFrom(selectedObjectType)) { SPWeb web = (SPWeb)obj; parentName = web.Name; definitions = web.EventReceivers; } else if (typeof(SPList).IsAssignableFrom(selectedObjectType)) { SPList list = (SPList)obj; parentName = list.Title; definitions = list.EventReceivers; } else { throw new NotSupportedException("The selected object type is not supported."); } if (definitions != null) { bool cancel = false; List <SPEventReceiverDefinition> pendingDelete = new List <SPEventReceiverDefinition>(); foreach (SPEventReceiverDefinition definition in definitions) { if ((definition.Type == eventType) && string.Equals(definition.Assembly, selectedAssembly.FullName) && string.Equals(definition.Class, selectedType.FullName)) { if (MessageBox.Show(parent, string.Format("The selected event receiver has already been hooked to the [{0}] objects [{1}] event. Do you wish to overwrite the existing event hook?", parentName, eventType), "Event Already Hooked", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) { pendingDelete.Add(definition); } else { cancel = true; break; } } if (definition.SequenceNumber > maxSequence) { maxSequence = definition.SequenceNumber; } } foreach (SPEventReceiverDefinition definition in pendingDelete) { definition.Delete(); } if (cancel) { return; } definitions.Add(eventType, selectedAssembly.FullName, selectedType.FullName); if (useNextSequenceNumber) { foreach (SPEventReceiverDefinition definition in definitions) { if ((definition.Type == eventType) && string.Equals(definition.Assembly, selectedAssembly.FullName) && string.Equals(definition.Class, selectedType.FullName)) { definition.SequenceNumber = (maxSequence + 1); definition.Update(); break; } } } } }