Yesterday I blogged about keeping System Account out of the Modified By column. Today I want to tackle the other annoying place System Account rears its ugly head – workflow history.
The LogToHistoryListActivity determines who it will log as by reading the ID in the UserId property. By default this property is set to –1, meaning the System Account. What we need to do is set this to the ID for the user. How we go about finding the user ID and assigning it will vary a bit.
If we’re looking for the person who kicked off the workflow, this is pretty easy. After dragging out your LogToHistoryActivity, bind the UserId property to workflowProperties.OriginatorUser.ID.
If you’re looking for the user who modified a task, you’ll need to perform a couple of steps.
- Add a field or property to the workflow to store the ID for the user of type Int32. I like to simply call mine logUserId.
- Create a new binding on the OnTaskChanged activity for the Executor property. This will contain the string for the login name. I call mind taskExecutor.
- Create a little helper method in the code to update the logUserId. I like mine UpdateLogUserId():
private void UpdateLogUserId()
{
logUserId =
workflowProperties.Web.EnsureUser(taskExecutor).ID;
} - Ensure the UpdateLogUserId() method is called before the LogToHistoryListActivity executes – this is most easily accomplished by making the call in the Invoked event handler for the OnTaskChanged event handler.
For each OnTaskChanged event handler and LogToHistoryActivity you simply need to ensure steps 1, 2 and 4 (since the method will have already been created) and you’re all set!
Now we can hide System Account from our users once and for all.
I want to get the user who changed the task, I followed same approach but it still returns me system account in the executer field. Please help.
ReplyDeleteI need a bit more information. :-) Are you talking about the history item or the task/workflow item?
ReplyDelete