Wednesday 9 May 2012

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.
//Creating a SPUsertoken object.
SPUserToken saUserToken = SPContext.Current.Site.SystemAccount.UserToken;
//passing to the constructor of the SPSite
SPSite spSite=new SPSite("siteURL",saUserToken);
Or we can use it like this.
SPUserToken saUserToken = SPContext.Current.Site.SystemAccount.UserToken;
SPSite spSite=new SPSite("siteURL");
//assigning the UserToken property of SpSite.
spSite.UserToken= saUserToken;  
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.


http://mssharepointtips.com/tip.asp?id=1022

http://code-journey.com/2010/run-code-with-elevated-privileges-in-sharepoint/

http://www.dannyhansen.nl/sharepoint/sharepoint-2010-elevated-privileges

http://msdn.microsoft.com/en-us/library/bb466220%28v=office.12%29.aspx

1 comment:

  1. You have covered the most important points in above post. My friend is preparing for an interview and this article will prove to be of great help to him. I will definitely pass the link.
    e-sign act

    ReplyDelete