Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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..

share|improve this question
    
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

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
    
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. –  Bernd Rickenberg 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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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