I have a re-usable out of the box sharepoint approval workflow 2010, and I am programmatically approve/reject workflow using the below code snippet inside infopath formcode (2010).
Hashtable ht = new Hashtable();
ht[SPBuiltInFieldId.Completed] = "TRUE";
ht["Completed"] = "TRUE";
ht[SPBuiltInFieldId.PercentComplete] = 1.0f;
ht["PercentComplete"] = 1.0f;
ht["Status"] = "Completed";
ht[SPBuiltInFieldId.TaskStatus] = SPResource.GetString
(new CultureInfo((int)task.Web.Language, false),
Strings.WorkflowStatusCompleted, new object[0]);
if (isApprove)
{
ht[SPBuiltInFieldId.WorkflowOutcome] = "Approved";
ht["TaskStatus"] = "Approved";
ht[SPBuiltInFieldId.Comments] = "Approved by Manager";
ht["Comments"] = "Approved by Manager";
}
else
{
XPathNavigator navigatorRejectComments
= this.MainDataSource.CreateNavigator().SelectSingleNode
(XPATH_REJECT_COMMENTS, this.NamespaceManager);
ht[SPBuiltInFieldId.WorkflowOutcome] = "Rejected";
ht["TaskStatus"] = "Rejected";
ht[SPBuiltInFieldId.Comments] = navigatorRejectComments.Value.Trim();
ht["Comments"] = navigatorRejectComments.Value.Trim();
}
ht["FormData"] = SPWorkflowStatus.Completed;
web.AllowUnsafeUpdates = true;
isApproveReject = AlterTask(task, ht, true, 5, 100);
web.AllowUnsafeUpdates = false;
Task Alter method
private static bool AlterTask(SPListItem task, Hashtable htData, bool fSynchronous, int attempts, int millisecondsTimeout)
{
if ((int)task[SPBuiltInFieldId.WorkflowVersion] != 1)
{
SPList parentList = task.ParentList.ParentWeb.Lists[new Guid(task[SPBuiltInFieldId.WorkflowListId].ToString())];
SPListItem parentItem = parentList.Items.GetItemById((int)task[SPBuiltInFieldId.WorkflowItemId]);
for (int i = 0; i < attempts; i++)
{
SPWorkflow workflow = parentItem.Workflows[new Guid(task[SPBuiltInFieldId.WorkflowInstanceID].ToString())];
if (!workflow.IsLocked)
{
task[SPBuiltInFieldId.WorkflowVersion] = 1;
task.SystemUpdate();
break;
}
if (i != attempts - 1)
Thread.Sleep(millisecondsTimeout);
}
}
return SPWorkflowTask.AlterTask(task, htData, fSynchronous);
}
This code works fine, but the comments of the task is not getting altered and it is not included in the e-mail as well. Is there anything wrong with ht["Comments"] that I use? It is not getting altered in task comments. This is SharePoint 2010 workflows and infopath form i am using is also 2010.
Anyone faced similar problem ?
To add a comment to a task when you Approve/Reject it, you just need to use the line before AlterTask:
After the task is approved you can see the comments in the Workflow History List.
You can also get all the consolidated comments from a task with:
Good luck!
If you need code only for Lists (not for DocLibs) you can use
item.ModerationInformation.Status
property. Like the the following example:But if you want to do it for all list types, you can use internal method
UpdateInternal
, with the following parameters:You can use examples from this russian blog Item ModerationInformation and SPListItem.UpdateInternal()