Approve a SharePoint workflow task using SharePoint Web Services / Object Model

不打扰是莪最后的温柔 提交于 2019-11-30 07:29:26

After a lot of trials and investigation I just had the following code working to approve the task

SPSite site = new SPSite("http://servername/");
using (SPWeb web = site.OpenWeb())
{
    SPList list = web.Lists["TestList"];
    SPListItem item = list.GetItemById(22);
    SPWorkflow workflow = item.Workflows[0];
    SPWorkflowTask task = workflow.Tasks[0];

    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]);
    ht[SPBuiltInFieldId.WorkflowOutcome] = "Approved";
    ht["TaskStatus"] = "Approved";
    ht["FormData"] = SPWorkflowStatus.Completed;

    web.AllowUnsafeUpdates = true;
    SPWorkflowTask.AlterTask((task as SPListItem), ht, true);
}

I suspect that ht["TaskStatus"] = "Approved"; is that attribute that solved it. Anyway I will try to narrow on the set of properties that need to be changed.

You can use the following code that uses the lists web service and the UpdateListItems method. The key is to use the Cmd='Moderate'

 public static XmlNode UpdateListItemApprove()
 {
            listservice.Lists listProxy = new listservice.Lists();


            string xml = "<Batch OnError='Continue'><Method ID='1' Cmd='Moderate'><Field Name='ID'/><Field Name='FileRef'>http://basesmcdev2/sites/tester1/approvals/KL022030.lic</Field><Field Name=\"_ModerationStatus\" >0</Field></Method></Batch>";

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            XmlNode batchNode = doc.SelectSingleNode("//Batch");

            listProxy.Url = "http://basesmcdev2/sites/tester1/_vti_bin/lists.asmx";
            listProxy.UseDefaultCredentials = true;

            XmlNode resultNode = listProxy.UpdateListItems("approvals", batchNode);

            return resultNode;


}

I'm not sure if Madhur's solution works on the associated item or on the task, but to update the task try:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<UpdateListItems
xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<listName>Tasks</listName>
<updates>
<Batch OnError="Continue" ListVersion="1">
<Method ID="1" Cmd="Update">
<Field Name="ID">199</Field>
<Field Name="Outcome">Approved</Field>
<Field Name="Status">Completed</Field>
<Field Name="ows_TaskStatus">Approved</Field>
</Method>
</Batch>
</updates>
</UpdateListItems>
</soap:Body>
</soap:Envelope>

Info on the service:

http://objectmix.com/sharepoint/800144-updatelistitems-web-service-does-not-update-field.html

Info on the approved field:

http://social.msdn.microsoft.com/Forums/en/sharepointworkflow/thread/6712d379-2df6-4223-9a29-b2e60493f1b6

http://social.msdn.microsoft.com/Forums/en/sharepointworkflow/thread/3dc95190-cc61-4067-ac35-2d1a82fad499

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!