Dela via


How to show task creator's & modifier's name instead of "System Account" in the Task Library

 

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.

Interesting!

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; 
    5:  
    6: namespace UpdateTaskColumns
    7:  
    8: {
    9:     public class UpdateColumns : SPItemEventReceiver
   10:  
   11:     {
   12:  
   13:         public override void ItemAdded(SPItemEventProperties properties)
   14:  
   15:         {
   16:             UpdateUserColumns(properties,false);       
   17:  
   18:         } 
   19:  
   20:         public override void ItemUpdated(SPItemEventProperties properties)
   21:         {
   22:             UpdateUserColumns(properties,true);
   23:  
   24:         } 
   25:  
   26:         private void UpdateUserColumns(SPItemEventProperties properties,bool IsUpdate)
   27:  
   28:         {
   29:  
   30:             SPWeb oWeb = properties.OpenWeb();
   31:  
   32:             Guid oSourceListID = new Guid(properties.ListItem["Workflow List ID"].ToString());
   33:  
   34:             SPList oSourceList = oWeb.Lists.GetList(oSourceListID, true);
   35:  
   36:             SPListItem oSourceListItem = oSourceList.GetItemById(Convert.ToInt32(properties.ListItem["Workflow Item ID"]));
   37:  
   38:             if (IsUpdate)
   39:  
   40:                 properties.ListItem["Editor"] = oSourceListItem["Editor"];
   41:  
   42:             else
   43:             {
   44:                 properties.ListItem["Author"] = oSourceListItem["Editor"];
   45:  
   46:                 properties.ListItem["Editor"] = oSourceListItem["Editor"];
   47:  
   48:             } 
   49:  
   50:             properties.ListItem.Update(); 
   51:  
   52:         }
   53:  
   54:     }
   55:  
   56: }

Code is self explanatory J

Happy coding!

Comments