Why we are using Run With Elevated Privileges in SharePoint ?
Loading
Why we are using Run With Elevated Privileges in SharePoint ?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Rajkiran SwainRank #165 · 14.4K reputationPosted Jun 20, 2017, 10:59 PM EDT
The RWEP method enables you to supply a delegate that runs a subset of code in the context of an account with higher privileges than the current user", this is the definition from Microsoft MSDN. Basically the code executed inside this method has "System Account" privileges in addition to the current user privileges or in a better way - we can tell that this method runs under the Application Pool identity, which has site collection administrator privileges on all site collections hosted by that application pool.
All the operations done inside this method should use a new SPSite Object. This means that the SPSite variables that are instantiated or created outside the subset code of this method can't be used for security elevation and it will result with the "Access denied error".
Correct Usage Sample:
The above mentioned code snippet is the exact usage of the RWEP and if we try to use the SPSite objects created outside the RWEP method like this...
Faulty Usage:
...it will give an “Access denied” error for operations that need higher level of access, because the user will be the currently logged in user, not the “System Account”
If we look inside the “Microsoft.SharePoint.dll” and look at the RWEP method internals, we can see that it uses the WIN32API for the impersonation of the current thread. Actually it doesn't create a new instance thread for this elevated code execution rather it just impersonates during elevated code execution and reverts back to the current user's identity after the elevated code execution. This can cause unpredictable/obscure issues with your application. Also RWEP doesn't allow changing the farm level or SSP level object properties.
Alternative impersonation technique to RunWithElevatedPrivileges() method:
A practical alternative to the RWEP method is to use SPUserToken of the SystemAccount directly with SPSite object. We can either create a new SPSite instance from the existing SPSite Object, whose constructor will be provided by the "UserToken" of the System Account or we can assign the "UserToken" property of the SPSite object, see the below code snippet.
Or we can use it like this.
In the above technique, there is no case of obscure bugs that may occur with RWEP and we can use the existing SPSite Objects for impersonation too, hence we can solve the restrictions when using the RWEP method.
Tapan MallickRank #3,323 · 432 reputationPosted Jun 20, 2017, 11:07 PM EDT
Thanks Rajkiran . Could you please provide me your contact no ?
I want to learn SharePoint from beginning .
Rajkiran SwainRank #165 · 14.4K reputationPosted Jun 20, 2017, 11:08 PM EDT