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)
    base.ItemAdding(properties);
    SPSecurity.RunWithElevatedPrivileges(delegate()
       {SPWeb web = properties.OpenWeb();
        base.ItemAdding(properties);
        string name= web.Lists[properties.ListId].Fields["first name"].InternalName; 
        var list = web.Lists["location names"]; 
  });}
link|improve this question

While not necessarily causing this behavior, do you need those two "base.ItemAdding(properties);" rows? – Jussi Palo Jan 23 at 19:05
thanks for pointing them out..i will get rid of those – spStacker Jan 23 at 19:16
1  
What do you mean "view this webpart"? Does the code not work for non-site admins? – tarjeieo Jan 24 at 7:50
@tarjeieo ..yes the non site admins were getting access denied – spStacker Jan 27 at 15:52
feedback

1 Answer

up vote 7 down vote accepted

You should recreate context.

SPSecurity.RunWithElevatedPrivileges(delegate() 
{
   using(SPSite site = new SPSite(properties.SiteId)
   {
      using(SPWeb web = site.OpenWeb(properties.Web.ID)
      {
          //code
      }
   }   
} 
link|improve this answer
@FreeStyler, what do you mean? – Alexander Jan 23 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 at 21:26
feedback

Your Answer

 
or
required, but never shown

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