Friday, December 11, 2020

Run With Elevated Privilege (RWEP)

RunWithElevatedPrivileges:-
SPSecurity.RunWithElevatedPrivileges() is used to execute some code that has to be run under some higher privileges.

The RWEP method enables us to supply a delegate that runs a subset of code in the context of an account with higher privileges than the current user. 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. In other words, whenever we use SPSecurity.RunWithElevatedPrivileges(), it will execute the code under the context of Application Pool identity.

If we are defining an Event Receiver, then we can use properties argument to get the URL.

                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (SPSite site = new SPSite(properties.Web.Url))
                        {
                            using (SPWeb web = site.OpenWeb())
                            {
                                // ... TO DO TASK
                            }
                        }
                    });



Additional Blogs:-
  1. https://blogs.msdn.microsoft.com/sowmyancs/2008/08/13/spsecurity-runwithelevatedprivileges-an-important-point-while-using-it-in-web-context/
  2. https://www.mssqltips.com/sqlservertip/3802/when-and-how-to-use-run-with-elevated-privileges-rwep-in-sharepoint/


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.