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 .. ..

Comments

Popular posts from this blog

Visual Studio Build Issue : Build: 0 succeeded or up-to-date, 0 failed, 1 skipped

SharePoint log error "Cannot find site lookup info for request Uri http://"

SharePoint PowerShell to Manage Term Store / Add Terms using PowerShell