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!
1: using System;
2: using System.Collections.Generic;
3: using System.Text;
4: using Microsoft.SharePoint;
6: namespace UpdateTaskColumns
9: public class UpdateColumns : SPItemEventReceiver
13: public override void ItemAdded(SPItemEventProperties properties)
20: public override void ItemUpdated(SPItemEventProperties properties)
26: private void UpdateUserColumns(SPItemEventProperties properties,bool IsUpdate)
30: SPWeb oWeb = properties.OpenWeb();
32: Guid oSourceListID = new Guid(properties.ListItem["Workflow List ID"].ToString());
34: SPList oSourceList = oWeb.Lists.GetList(oSourceListID, true);
36: SPListItem oSourceListItem = oSourceList.GetItemById(Convert.ToInt32(properties.ListItem["Workflow Item ID"]));
38: if (IsUpdate)
40: properties.ListItem["Editor"] = oSourceListItem["Editor"];
44: properties.ListItem["Author"] = oSourceListItem["Editor"];
46: properties.ListItem["Editor"] = oSourceListItem["Editor"];
Code is self explanatory J
PingBack from http://wordnew.acne-reveiw.info/?p=1104
I tried changing the "Modified By" field using this code but it dint worked.
properties.ListItem["Editor"] = oSourceListItem["Editor"];
I checked out that it throws an exception saying - "The field you are trying to update may be read only".
It would be really helpful if you could tell how to proceed further.
Please check the below post and let me know if you have any queries.
Where is the event handler that I am changing?