Intranoggin

Blither, Blather, Web Content Management.
Blog » Custom Designer Workflow Impersonation

Custom Designer Workflow Impersonation

You know that all the built in SharePoint Designer workflow activities (declarative workflows) impersonate the user that started the workflow.

If you write your own SPD workflow activities, and you want them to behave the same way, then don't forget to pass CurrentUser.UserToken into any new SPSites you are using.

Consider the below example of an Execute function that sets the title of a site. If you change this line:

using (SPSite site = new SPSite(SiteURL, __Context.Web.CurrentUser.UserToken))

to this:

using (SPSite site = new SPSite(SiteURL))

then the code that updates the title will be running as your workflow's privileged account, just as though you were using a RunWithElevatedPriviledges block.

SPSecurity.RunWithElevatedPrivileges(delegate(){ });

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

{

try

{

using (SPSite site = new SPSite(SiteURL, __Context.Web.CurrentUser.UserToken))

{

using (SPWeb web = site.OpenWeb())

{

if (string.IsNullOrEmpty(NewTitle))

{

NewTitle = "";

}

string oldTitle = web.Title;

web.Title = NewTitle;

web.Update();

string message = "Site: " + SiteURL + "; renamed from " + oldTitle + " to " + NewTitle;

WorkflowHistoryLogger.LogMessage(executionContext, SPWorkflowHistoryEventType.None, "Complete", UserID, message);

}

}

}

catch (Exception ex)

{

WorkflowHistoryLogger.LogError(executionContext, UserID, ex);

return ActivityExecutionStatus.Faulting;

}

return ActivityExecutionStatus.Closed;

}


Posted: 6/29/2009 9:27:00 AM by Ryan Miller | with 0 comments
Filed under: SharePoint