Monday, October 4, 2010

Impersonating the Sharepoint System Account

Impersonating the SharePoint System Account is pretty simple. All you need is an the SPWeb object and you ready to go.

Here is how to do it.

using (SPWeb web = properties.OpenWeb())
{
var adminUser = web.AllUsers[@"SHAREPOINT\SYSTEM"];
var adminToken = adminUser.UserToken;

using (SPWeb site = new SPSite(web.Url, adminToken).OpenWeb())
{
/* Now running in the context of the system account */
}
}

And there it is, you can now run your code as the SharePoint System Account.

No comments:

Post a Comment