I have a strong gut feeling that using SharePoint RunWithElevatedPrivileges
should be avoided like the plague, but need to convince some others as to exactly why. Here's what I have.
Reasons to elevate fall into two categories:
For the former, you're much better off using SPSite impersonation. The latter is the only reason I ever use RWEP.
To clarify, RWEP does not spawn a new thread. Instead it uses Win32 APIs to revert the current thread's identity back to the process identity (turning off impersonation) to run the elevated code, then switch impersonation back on to resume work on behalf of the current user. This has several implications:
And as Alex said, children of an SPSite inherit their permissions from the SPSite, which in turn has its permissions set when it is created. So SPContext.Current.Site will still behave with the permissions of the current user even if you reference it within your CodeToRunElevated. Instead, you would need to create and consume a new SPSite within the elevated block.
To summarize: RWEP to impersonate the App Pool outside of SharePoint, SPSite impersonation to impersonate the App Pool inside of SharePoint.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With