1

Is there a way to get items from a list even if the current user does not actually have acces to them? SPSecurity.RunWithElevatedPrivileges does not seem to work!

I have a special case in which I need to access some items from a list, items that respect a certain filter. The filter can return items that belong to other users and these are not returned.

But I need them to collect some information and display it to the user! The user won't see the items to which he does not have access to, but the information I'm displaying needs to take them into account.

Is it possible to do this without setting/removing permissions programmatically?

1 Answer 1

4

Well, SPSecurity.RunWithElevatedPrivileges should do the trick. Make sure, you create a new SPSite/SPWeb within the elevated code block:

var siteId = SPContext.Current.Site.ID;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
  using (SPSite site = new SPSite(siteId))
  {
    //do things on the new site object
  }
});
3
  • 1
    +1 for reminding me that the objects retrieved outside the delegate code run with the outside context and not under privileged context. My brain stepped out for a moment there...
    – JohnDoDo
    May 14, 2012 at 12:52
  • Your code snippet isn't disposing of the SPSite you create, you need to have both the site and web creating in a using block.
    – Servy
    May 14, 2012 at 16:33
  • @Servy - I can't see a problem in that code - the using will dispose of the SPSite he's created and he hasn't created SPWeb.
    – Ryan
    May 15, 2012 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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