Information Worker Solutions (Intranet, Office System, Enterprise Content Management)

Impersonation in SharePoint 2007

In SharePoint 2003 there was no easy way to use impersonation. In SharePoint 2007 there is a nice and easy built-in way to use impersonation:

SPSite site = new SPSite("SiteCollection_Url");
SPWeb web = site.OpenWeb();
SPUser user = web.AllUsers["User_Name"];

SPUserToken token = user.UserToken;
SPSite impersonatedSiteCollection = new SPSite("SiteCollection_Url", token);

You can also check whether an SPSite object is using impersonation:

SPSite siteCollection = SPControl.GetContextSite(HttpContext.Current);
bool impersonating = siteCollection.Impersonating;

Keep in mind that the user you use to impersonate should be a site collection administrator. Otherwise you will get an Access Denied or Unknown User error while trying to impersonate.

Published Monday, March 19, 2007 2:26 PM by Mirjam

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Rick Smit said:

You can also use the new SPSecurity.RunWithElevatedPrivileges Method.

Be carefull: Executes the specified method with Full Control rights even if the user does not otherwise have Full Control.

More information: http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx
July 4, 2007 1:38 PM

What do you think?

(required) 
(optional)
(required) 

Enter Code Here: Required

About Mirjam

Mirjam van Olst werkt als SharePoint Architect voor het Information Worker Solution center van Macaw. Vanaf 2004 heeft Mirjam bedrijven in alle soorten en maten geholpen bij het implementeren van portal, ECM en search oplossingen op basis van verschillende SharePoint versies. Mirjam is één van de 45 Microsoft Certified Masters SharePoint 2007 wereldwijd. Daarnaast is zij één van de 22 kandidaat Microsoft Certified Masters SharePoint 2010. Mirjam doet veel werk in de SharePoint community. Zo maakt zij deel uit van de Dutch Information Worker User Group (DIWUG) organisatie. In deze rol is zij editor en auteur van het populaire DIWUG eMagazine. Daarnaast schrijft zij ook artikelen voor het SDN magazine, het Nederlandse .Net Magazine en het Nederlandse TechNet Magazine. Mirjam spreekt regelmatig op nationale en internationale conferenties en events en heeft een blog op http://sharepointchick.com. In januari 2010 heeft Mirjam de Microsoft Most Valuable Professional (MVP) award gekregen.

This Blog

Post Calendar

<March 2007>
SuMoTuWeThFrSa
25262728123
45678910
11121314151617
18192021222324
25262728293031
1234567

Syndication

Powered by Community Server, by Telligent Systems