Wednesday, December 7, 2011

How to impersonate SharePoint Programmatically

There are many occurrences that we need to impersonate our SharePoint requests to server. Otherwise SharePoint will use system account or context credentials to perform the task.

In my case i want to programmatically approve a workflow task. but then i noticed it is showing modifiedby as   system account so i used following code to change the user credentials to intended user.

SPUserToken ApprovingUser = web.GetUserToken(“domain\username”);

using (SPSite site = new SPSite(“Site Url”, ApprovingUser))
{
using (SPWeb wb = site.OpenWeb())
{
/* Do Anything You want */
}
}
Post a Comment