I created one event receiver and deployed it, but only users who are site collection admins are able to view this web part. I have included the SPSecurity.RunWithElevatedPrivileges but the same thing. What am I missing?

Below is my code

 public override void ItemAdding(SPItemEventProperties properties)
       {SPWeb web = properties.OpenWeb();
        string name= web.Lists[properties.ListId].Fields["first name"].InternalName; 
        var list = web.Lists["location names"]; 
  • While not necessarily causing this behavior, do you need those two "base.ItemAdding(properties);" rows?
    – Jussi Palo
    Jan 23 '12 at 19:05
  • 1
    What do you mean "view this webpart"? Does the code not work for non-site admins?
    – tarjeieo
    Jan 24 '12 at 7:50
  • @tarjeieo ..yes the non site admins were getting access denied
    – spStacker
    Jan 27 '12 at 15:52

You should recreate context.

   using(SPSite site = new SPSite(properties.SiteId))
      using(SPWeb web = site.OpenWeb(properties.Web.ID))
  • @FreeStyler, what do you mean?
    – Alexander
    Jan 23 '12 at 21:20
  • Sorry, I meant is whenever we use 'RunWithElevatedPrviliges', we have to take the current context outside the SPSecurity.RunWithElevatedPrivileges block and then create a new instance of SPSite and SPWeb inside the that block which will run under application pool identity. The code above is exactly doing so! Jan 23 '12 at 21:26

Please make sure that your solution is a farm solution, not a sandbox solution.

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.