Whenever we create a SharePoint site we will get a default list called “Tasks”. This is one of great features available in SharePoint. Tasks list is being used to track workflow tasks in SharePoint. So, whenever we start a workflow and assign a task to somebody, and if he or she approve or reject it all those task activities will track and log in to the Task list.
That is all about the Task list. Now we can come to the reason why I am writing this blog post. We all know that whenever a task item added to a Task list, the “created by” and “modified by” columns will show the value “system account” always!
Once I got cool requirement from one my customers, he doesn’t want to display the “System account” as the default creator or the modifier in his task list. He want to show the name of the user who has created the task, in the “created by” column and if somebody modify that task by approving/rejecting that workflow task, then that person’s name has to show in the “modified by” column.
As a work-around what I did that, I have created an event handler – ItemAdded & ItemUpdated, which will capture the event whenever a new task Item added to the Task List and whenever we modify that task by approving/rejecting the workflow task.
And whenever we add or edit a task item, I just tried to update the “created by” and “modified by” columns using SPWeb.CurrentUser property. But, then I have found that, though a different user log in to the application, in the event handler that property returning the current user as “System Account”.
More info on how to update the “created by” and “modified by” columns, please see this post.
After digging into the workflow task creation process, I have found that all the tasks are creating and adding to the Task list under the Application pool identity and that is why we are getting SPWeb.CurrentUser value as “System account”.
I was really upset because of this and in one point I had felt that we can’t update the “created by” and “modified by” columns L. But I believed that there will be some hidden treasure in SharePoint which will help us really to accomplish our customization requirements.
I just checked the fields available for the Tasks list then I have found that, there were 2 fields:
Workflow List ID = which will return the GUID of the List from which workflow task has been created.
Workflow Item ID = which will return the list item from which workflow task has been created.
Cooool! Once we get those 2 fields then no need to worry anymore!
I just modified my existing custom event handler code as like below and it worked like charm!
Code is self explanatory J