以编程方式批准/拒绝工作流程并添加注释

时间:2012-07-18 20:56:04

标签: c# sharepoint sharepoint-2010 sharepoint-workflow sharepoint-api

我正在尝试以编程方式批准/拒绝SharePoint中的工作流程。我可以成功地做到这一点,但我无法添加评论。我从一年old question得到了我的代码,但也没有回答,所以我想我会开始一个新的问题。

我的代码:

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 (param == "Approved")
{
    ht[SPBuiltInFieldId.WorkflowOutcome] = "Approved";
    ht["TaskStatus"] = "Approved";
    if (!string.IsNullOrEmpty(comments))
    {
        ht[SPBuiltInFieldId.Comments] = comments;
        ht["Comments"] = comments;
        ht[SPBuiltInFieldId.Comment] = comments;
    }
}
else
{

    ht[SPBuiltInFieldId.WorkflowOutcome] = "Rejected";
    ht["TaskStatus"] = "Rejected";
    if (!string.IsNullOrEmpty(comments))
    {
        ht[SPBuiltInFieldId.Comments] = comments;
        ht["Comments"] = comments;
        ht[SPBuiltInFieldId.Comment] = comments;
    }
}
ht["FormData"] = SPWorkflowStatus.Completed;
bool isApproveReject = AlterTask(task, ht, true, 5, 100);

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);
}

2 个答案:

答案 0 :(得分:3)

要在批准/拒绝任务时向任务添加注释,只需在AlterTask之前使用该行:

ht["ows_FieldName_Comments"] = comments;

任务批准后,您可以在工作流历史记录列表中看到评论。

您还可以通过以下方式获取任务的所有合并评论:

Hashtable extProperties = SPWorkflowTask.GetExtendedPropertiesAsHashtable(currentTask);

string consolidatedComments = extProperties["FieldName_ConsolidatedComments"].ToString();

答案 1 :(得分:1)

也可以批准/拒绝工作流程任务客户端对象模型

批准工作流程任务的代码

        ClientContext ctx = new ClientContext("http://SiteUrl");
        Web web = ctx.Web;
        List list = web.Lists.GetByTitle("My Task List");
        ListItem listitem = list.GetItemById(1);
        listitem["Completed"] = true;
        listitem["PercentComplete"] = 1;
        listitem["Status"] = "Approved";
        listitem["WorkflowOutcome"] = "Approved";
        listitem.Update();
        ctx.ExecuteQuery();

拒绝工作流程任务的代码

        ClientContext ctx = new ClientContext("http://SiteUrl");
        Web web = ctx.Web;
        List list = web.Lists.GetByTitle("My Task List");
        ListItem listitem = list.GetItemById(1);
        listitem["Completed"] = false;
        listitem["PercentComplete"] = 1;
        listitem["Status"] = "Rejected";
        listitem["WorkflowOutcome"] = "Rejected";
        listitem.Update();
        ctx.ExecuteQuery();