03 November 2009

Workflow LogToHistoryList UserID and Task Activity Executor in SharePoint

This topic is within a SharePoint 2007 workflow, how do we get the Workflow History to reflect the user who modified a task, rather than the "System Account".  I've successfully implemented the solution suggested by Julie Kramer, from the office dev blog.  Here we go:

snippet of a workflow design in VS2008 - focus on the two red activities




  1. Assign a new field to the Executor property of the OnTaskChanged activity in question (you'll see this property in the property window on the activity). Note we use this new field in a later LogToHistoryListActivity's Method_Invoking() by assigning the UserID property of the LogToHistoryListActivity's object





  2. Add a userid lookup/helper function to guarantee a valid SharePoint user id is in place
        private SPUser GetUserObject(string accountID)
        {
            try
            {
                if (accountID.IndexOf(@"\") > 0)
                {
                    SPUser user = this.workflowProperties.Web.SiteUsers[accountID];
                    return user;
                }
                else
                {
                    //replace DOMAIN 
                    SPUser user = this.workflowProperties.Web.SiteUsers[@"DOMAIN\" + accountID];
                    return user;
                }
            }
            catch
            {
                //replace DOMAIN and administrator
                SPUser adminUser = this.workflowProperties.Web.SiteUsers[@"DOMAIN\administrator"];
                return adminUser;
            }
        }
As you assign a new field to each TaskChangedEvent's executor property, you'll see this declaration inserted to the bottom of the workflow class.


public String RVPReviewTaskChangedEvent_Executor1 = default(System.String); 

Later when the LogToHistoryListActivity's Method_Invoking event is triggered, you use this new field and the helper function to modify the UserID property on the LogToHistoryListActivity's object;



        private void logRVPReviewed_MethodInvoking(object sender, EventArgs e)
        {
            SPUser executor = GetUserObject(RVPReviewTaskChangedEvent_Executor1);
            logRVPReviewed.UserId = executor.ID;

        }

2 comments:

  1. you can do it, just with this code...

    Contact executor = Contact.FromName(TaskChanged_Executor,workflowProperties.Web);
    CurrentWorkflowUserId = executor.PrincipalID;

    ReplyDelete
  2. Great Job.

    Thanks,
    Sri

    ReplyDelete

firstly, thanks in advance for your comment - I don't get very many, so I'm sure to follow up if you leave me a way - secondly, come again.