Home > development, error, event handlers, MOSS, MS SharePoint Sever, sharepoint, sps > Update a Task item in Tasks List

Update a Task item in Tasks List

This is how you can update a task item in a tasks list:

There is a list item.
It has a “Request Created” date.
It has a “Due Date”.
It has a workflow running on it. The workflow creates a task in the Tasks list.
The task list also has columns added as “Request Created” and “Due Date”.
We are copying “Request Created” and “Due Date” from the list item into the task in the Tasks list.


private static void UpdateTask(SPWeb web, SPListItem refReqItem)
{
  try
  {
      web.Site.AllowUnsafeUpdates = true;
      SPWorkflowCollection itemWorkflowCollection = refReqItem.Workflows;
      foreach (SPWorkflow itemWorkflow in itemWorkflowCollection)
      {
          if (!itemWorkflow.IsCompleted && itemWorkflow.InternalState == SPWorkflowState.Running)
          {
               SPWorkflowTaskCollection wfTasks = itemWorkflow.Tasks;
               foreach (SPWorkflowTask wfTask in wfTasks)
              {
                   Hashtable htData = new Hashtable();
                   htData[RefReqsTasksFieldNames.RefReqsTasks_RequestCreated] = refReqItem[ReferenceReqsFieldNames.RefReqs_RequestCreated]; //htData[taskFieldName] = listItem[listFieldName]

                  htData[RefReqsTasksFieldNames.RefReqsTasks_DueDate] = refReqItem[ReferenceReqsFieldNames.RefReqs_DueDate]; //htData[taskFieldName] = listItem[listFieldName]

                  SPWorkflowTask.AlterTask((wfTask as SPListItem), htData, true);

             }
       }
    }
 }
 catch (Exception ex)
 {
     Log.LogError("Error in ItemAdded:UpdateTask Method", ex);
 }
}

Call the funciton like this:

Before calling this funciton, make sure that event firing is ENABLED. That’s because SPWorkflowTask.AlterTask() needs to fire events……


UpdateTask(web, refReqItem);

So, now, the above code will run for all the tasks created by all the workflows running on a particular item. We usually dont want that. We want to modify a particular task created by a particular workflow. So, do this:

Just add an additional condition that will get each workflow name and compare with the workflow we are after. Once the names match, update the task, otherwise dont update:


foreach (SPWorkflow itemWorkflow in itemWorkflowCollection)
{
string refReqAppWF_Name = itemWorkflow.ParentAssociation.Name.ToString();
if (refReqAppWF_Name == Config.RefReqApprovalWF_Name)

where, Config.RefReqApprovalWF_Name is the workflow we are looking for. So now, only that task will be updated which is created by this workflow.

Full method below:

private static void UpdateTask(SPWeb web, SPListItem refReqItem)
        {
            try
            {
                web.Site.AllowUnsafeUpdates = true;
                SPWorkflowCollection itemWorkflowCollection = refReqItem.Workflows;
                foreach (SPWorkflow itemWorkflow in itemWorkflowCollection)
                {
                    string refReqAppWF_Name = itemWorkflow.ParentAssociation.Name.ToString();
                    if (refReqAppWF_Name == Config.RefReqApprovalWF_Name)
                    {
                        if (!itemWorkflow.IsCompleted && itemWorkflow.InternalState == SPWorkflowState.Running)
                        {
                            SPWorkflowTaskCollection wfTasks = itemWorkflow.Tasks;
                            foreach (SPWorkflowTask wfTask in wfTasks)
                            {
                                Hashtable htData = new Hashtable();
                                htData[RefReqsTasksFieldNames.RefReqsTasks_RequestCreated] = refReqItem[ReferenceReqsFieldNames.RefReqs_RequestCreated];
                                htData[RefReqsTasksFieldNames.RefReqsTasks_DueDate] = refReqItem[ReferenceReqsFieldNames.RefReqs_DueDate];
                                SPWorkflowTask.AlterTask((wfTask as SPListItem), htData, true);

                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.LogError("Error in ItemAdded:UpdateTask Method", ex);
            }
        }

Hope this helps–
Rehman.

Advertisements
  1. SV
    October 27, 2012 at 8:02 pm

    How do you reassign a task programmatically?

  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: