Get list items even if user does not have access to those items
-
09-12-2019 - |
Question
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?
Solution
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
}
});