This is Sample of my Code

SPList list = new SPSite(siteURL).OpenWeb().Lists[listName];
SPEventReceiverDefinitionCollection eventReceivers = list.EventReceivers;

SPEventReceiverDefinition eventReceiver = eventReceivers.Add();

but when i use this code it returns error while Build Project and error is

Error 1 'Microsoft.SharePoint.SPEventReceiverDefinitionCollection' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'Microsoft.SharePoint.SPEventReceiverDefinitionCollection' could be found (are you missing a using directive or an assembly reference?)

What Should I do now? can anybody help me Its Urgent.. Its a Sandboxed Solution..

  • I m facing same problem.. will you please tell how did you get solve this in your case? I m trying to add event receiver in Feature activating code. Thanks in advance.. :)
    – Gaurav
    Jan 11 '13 at 5:22
  • I just changed my project from Sandboxed to Farm.
    – Torque
    Jan 11 '13 at 5:32
  • Thanks for reply. I need to keep my solution Sandbox only , as i need to use that eventhandler in office 365. so to upload solution through Sitesettings - Solutions we need to have Sandbox solution
    – Gaurav
    Jan 11 '13 at 5:38

The code compiles and runs fine for me as long as there is a reference to Microsoft.SharePoint (2010). Are you building a console application? In that case make sure you are using the .NET 3.5 framework (not compact or anything else) and set the platform target to Any CPU in the build section of the project properties.

BTW. note that you are leaking a site collection and a web object. You should looking at disposing both the SPSite and SPWeb instances.

  • I have written this code in Feature Receiver.
    – Torque
    Oct 30 '12 at 8:06
  • Its working fine for Farm Solution but I want it for Sandboxed Solution.
    – Torque
    Oct 30 '12 at 8:27
  • 2
    You cannot use the Add method or any of the overrides in a sandboxed solution. This is stated in the MSDN documentation: msdn.microsoft.com/en-us/library/ms430598.aspx. You can only add event receivers declaratively here. See the comment on the referenced MSDN page. Oct 30 '12 at 8:40
  • Thanx Bernd, I got solution which I was looking for.
    – Torque
    Oct 30 '12 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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