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

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"]; 
  });}
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
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
add comment

1 Answer 1

up vote 9 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
      }
   }   
});    //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
add comment

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.