I am using SP2013 On Prem. I use a third party solution that deploys custom lists to a site when activated. I need to remove the SPListItem ItemDeleting event receiver from this list, delete some items, and then re add this event receiver. This is in QA and we need a way to purge items during testing and the vendors event handler prevents this. This custom action will be used by 2 or 3 people in a non production farm.

I am able to add/remove EventReceivers(ER) using powershell without a problem but in C# it is not so clear to me how to add the ER back!

Prior to removal I store the Assembly, Class, Type and Name into a text file. In my AddEventReceiver function I want to retrieve these from the text file (easy part) and then readd the event handler to the list. I could always deactivate and reactivate the feature but I'm hoping for something more elegant (?).

Can someone show me how this can be accomplished?

1 Answer 1


Run inside SPSecurity.RunWithElevatedPrivileges:

var list = web.Lists.TryGetList(listName);
string[] lines = System.IO.File.ReadAllLines(@"path to txt file")

SPEventReceiverDefinition sper = list.EventReceivers.Add();
sper.Assembly = lines[0];
sper.Class = lines[1];
sper.Type = SPEventReceiverType.ItemDeleting;
sper.Name = lines[3];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.