0

I have this Situation:

I've made a list and added an event receiver to it. This event receiver changes some fields and adds new data(automatically filled date etc.).

The Problem:

The client wants some old data added to the list. When we add it, the Event receiver fires and changes some of the data(dates, some fields etc.) as it is supposed to and for the old data that is a problem. Another problem is that I can not modify the code of the receiver itself.

What I Tought:

I want to create a console application that gets the old data and inserts it in the list. The problem is that I don't know how to disable the event receiver in this particular case.

*I know that this "solution" can cause tons of security issues, but it's my best option for now.

All ideas are welcome.

3
  • Can you write Sharepoint server object model code in the console or is it Client object model in the console? If you have server access, you can try writing code to remove the event receiver, then run your code to update items and then run the code to attach event receiver back
    – Unnie
    Mar 19, 2014 at 10:33
  • Or if you know which feature is attaching the event receiver to the list, deactivate the feature, run your console to update list and activate that feature again
    – Unnie
    Mar 19, 2014 at 10:33
  • Or create a different content type and allow the list to accept it, then change the event receiver to skip the new content type.
    – Nico
    Mar 19, 2014 at 10:39

2 Answers 2

4

You can remove the event receivers, add your items and then add the event receivers back. You can do that with PowerShell or C#.

With C#:

To remove the event receivers loop through list.EventReceivers and delete the one you want (SPEventReceiverDefinition.Delete())

To add an event receiver:

SPEventReceiverDefinition eventReceiver = list.EventReceivers.Add();
eventReceiver.Name = "EventName";
eventReceiver.Type = SPEventReceiverType.ItemAdded; //or any other type
eventReceiver.SequenceNumber = 10000;
eventReceiver.Assembly = "YourAssembly,Version=1.0.0.0, Culture=neutral, PublicKeyToken=....(your token)";
eventReceiver.Class = "YourClassWithTheEventReceivers";
eventReceiver.Update();

You can do this in the same way with PowerShell: http://naimmurati.wordpress.com/2012/03/22/add-modify-or-delete-list-event-receivers-with-powershell/

1

Approach 1: if you know which feature is attaching the event receiver to the list, deactivate the feature, run your console to update list and activate that feature again.

Approach 2: Write code to remove the event receiver, then run your code to update items and then write another code to attach event receiver back.

See Remove Event receiver

See Attach event receiver to list

Your Answer

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

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