Home > C#, SharePoint, SharePoint 2010 > SharePoint – Using C# change workflow task owner

SharePoint – Using C# change workflow task owner

Assume we have a workflow task as below

sc1

Currently its assigned to “Amal Hashim”. We can change this to another user say, “Administrator” using following snippet

using (SPSite site = new SPSite("http://myteamsite:2012/"))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPUser user = web.SiteUsers["myad\\administrator"];
        SPListItem taskItem = web.Lists["Shared Documents"].Items[0];

        SPWorkflowTaskCollection taskCollection = new 
SPWorkflowTaskCollection(taskItem, new SPWorkflowFilter());

        foreach (SPWorkflowTask task in taskCollection)
        {
            Hashtable ht = new Hashtable();
            ht[SPBuiltInFieldId.AssignedTo] = user;
            SPWorkflowTask.AlterTask((task as SPListItem), ht, true);
        }
    }
}

After executing this, we will get

sc2

Advertisements
Categories: C#, SharePoint, SharePoint 2010 Tags:
  1. February 19, 2013 at 3:09 AM

    I had to share this unique article, “SharePoint – Using C# change workflow task owner .
    Net Goodies” with my best good friends on facebook.
    Isimply wanted to pass on ur superb writing! Thank you, Harlan

    • inturi
      April 27, 2013 at 2:37 AM

      Nice atricle. I am able to change the approver but when i try to approve as the new approver i am getting below error

      Error in task. Unauthorized attempt to update workflow task by usarxi10. The response has not been recorded.

      Could you please suggest what needs to be done. Here is my code

      SPList taskList = web.Lists[“Workflow Tasks”];
      SPListItem taskItem = taskList.GetItemById(taskId);

      int decision = 1;
      string comments = “test from c# code”;

      Guid commentsFieldId = Nintex.Workflow.Common.NWSharePointObjects.FieldComments;
      Guid decisionFieldId = Nintex.Workflow.Common.NWSharePointObjects.FieldDecision;
      Guid outcomeId = Nintex.Workflow.Common.NWSharePointObjects.FieldApprovalOutcome;

      taskItem[decisionFieldId] = decision;
      taskItem[commentsFieldId] = comments;
      taskItem[outcomeId] = 0;
      taskItem.Update();

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: