SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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"]; 
share|improve this question
While not necessarily causing this behavior, do you need those two "base.ItemAdding(properties);" rows? – Jussi Palo Jan 23 '12 at 19:05
thanks for pointing them out..i will get rid of those – spStacker Jan 23 '12 at 19:16
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
up vote 11 down vote accepted

You should recreate context.

   using(SPSite site = new SPSite(properties.SiteId)
      using(SPWeb web = site.OpenWeb(properties.Web.ID)
});    //Edited for this bracket   
share|improve this answer
@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! – Falak Mahmood Jan 23 '12 at 21:26

Your Answer


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.