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
    Commented Jan 23, 2012 at 19:05
  • 1
    What do you mean "view this webpart"? Does the code not work for non-site admins?
    – tarjeieo
    Commented Jan 24, 2012 at 7:50
  • @tarjeieo ..yes the non site admins were getting access denied
    – SandeshR
    Commented Jan 27, 2012 at 15:52

2 Answers 2


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
    Commented Jan 23, 2012 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! Commented Jan 23, 2012 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 and acknowledge you have read our privacy policy.

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