Update task item programatically in Sharepoint wit

2019-08-25 01:09发布

问题:

I want to update a task item programatically in CSOM. Item is updating but workflow is not triggering. I need just to open the item in sharepoint and save it. Then workflow is triggering.

                    List requestTasksList = MyWeb.Lists.GetByTitle("TestRequest Tasks");

                    List<TestRequestModel> testRequestList = new List<TestRequestModel>();

                    ListItemCollection ColListItems = requestTasksList.GetItems(Spqur);

                    ctx.Load(ColListItems);
                    ctx.ExecuteQuery();

                    foreach (ListItem task in ColListItems)
                    {
                        task["Status"] = "Completed";
                        task["TaskOutcome"] = "Approved";
                        task["PercentComplete"] = 1.0;
                        task["Checkmark"] = 1;
                        task.Update();
                        requestTasksList.Update();
                    }
                    ctx.ExecuteQuery();

This is the updated task item

As i said, When i click to save button, workflow is triggering and new task is creating.

回答1:

I'm not sure if its typo but it should be

List requestTasksList = MyWeb.Lists.GetByTitle("TestRequest Tasks");

List<TestRequestModel> testRequestList = new List<TestRequestModel>();

ListItemCollection ColListItems = requestTasksList.GetItems(Spqur);

foreach (ListItem task in ColListItems)
{
  task["Status"] = "Completed";
  task["TaskOutcome"] = "Approved";
  task["PercentComplete"] = 1.0;
  task["Checkmark"] = 1;
  task.Update();
}
ctx.ExecuteQuery();



回答2:

We needed to do the same thing and have found that there are no event handlers on the workflow tasks list in SharePoint 2013. I know that there is a SPWorkflowAutostartEventReceiver on lists that have workflows auto start on add or update, so I assumed this same approach would be done for workflow tasks as well, but it is not. Since there are no event handlers on the workflow tasks list, I surmise that all workflow triggers are initiated from the server-side UI code on the task list (horrible design).

For us we need to work completely client side with no farm solution or sandboxed code. So our only solution has been to screen scrape URLs and then open pages or dialogs for the user to do things like cancel all tasks for an approval workflow. Granted, this approach still does requires user input. I suppose you could screen scrape the whole page and play back the action of hitting buttons on a task page or cancel task page if you needed to avoid user input. That would be a pain, though.