Tuesday, December 1, 2009

Impersonate User in Sharepoint/MOSS 2007

We faced a strange problem in MOSS 2007 Application.
A user need to access the SPUserCollection object of the Parent site where the user is having "Read" right only.

We tried RunWithElevatePriviledges(), but it didn't work for us (Access denied exception).

Finally came out with a solution where we impersonate the user account to the default sharepoint System Account using a UserToken object. Things started working fine. Sample code is pasted below.

SPUser AdminUser = SPContext.Current.Web.AllUsers[@"SHAREPOINT\SYSTEM"];
//This is the default system account.

var superToken = AdminUser.UserToken;
//User Token Generated.

//Now use this token and perform any operation from the object model.

using (SPSite siteCollection = new SPSite(siteURL, superToken))
//Some Operation

Hope this helped.

Enjoy Coding .. ..

Custom UserControls/WebParts in SharePoint/MOSS 2007


This article describes the way to use custom webpart/usercontrol in SharePoint/MOSS.

Visual Studio Build Error - The "GenerateResource" task failed unexpectedly

Error: "The GenerateResource task failed unexpectedly" This error is quite common while building the project in Visual Studio....