tiago2014
tiago2014

Reputation: 3461

How to programmatically set the task outcome (task response) of a Nintex Flexi Task?

Is there any way of set a Nintex Flexi task completion through Sharepoint's web services? We have tried updating the "WorkflowOutcome", "ApproverComments" and "Status" fields without success (actually the comments and status are successfully updated, however I can find no way of updating the WorkflowOutcome system field).

I can't use the Nintex Web service (ProcessTaskResponse) because it needs the task's assigned user's credentials (login, password, domain).

The Asp.net page doesn't have that information, it has only the Sharepoint Administrator credentials. One way is to delegate the task to the admin first, and then call ProcessTaskResponse, but it isn't efficient and is prone to errors.


In my tests so far, any update (UpdateListItems) to the WorkflowOutcome field automatically set the Status field to "Completed" and the PercentComplete field to "1" (100%), ending the task (and continuing the flow), but with the wrong answer: always "Reject", no matter what I try to set it to.

Upvotes: 3

Views: 5685

Answers (2)

Phan Đức Bình
Phan Đức Bình

Reputation: 141

Here are my code to change outcome of nintex flexi task. My problem is permission. I had passed admin token to site. It's solve the problem.

           var siteUrl = "...";
            using (var tempSite = new SPSite(siteUrl))
            {
                var sysToken = tempSite.SystemAccount.UserToken;
                using (var site = new SPSite(siteUrl, sysToken))
                {
                    var web = site.OpenWeb();
                    ...

                                    var cancelled = "Cancelled";
                                    task.Web.AllowUnsafeUpdates = true;
                                    Hashtable ht = new Hashtable();
                                    ht[SPBuiltInFieldId.TaskStatus] = SPResource.GetString(new CultureInfo((int)task.Web.Language, false), Strings.WorkflowStatusCompleted, new object[0]);
                                    ht["Completed"] = true;
                                    ht["PercentComplete"] = 1;
                                    ht["Status"] = "Completed";
                                    ht["WorkflowOutcome"] = cancelled;
                                    ht["Decision"] = CommonHelper.GetFlexiTaskOutcomeId(task, cancelled);
                                    ht["ApproverComments"] = "cancelled";
                                    CommonHelper.AlterTask((task as SPListItem), ht, true, 5, 100);

                                    task.Web.AllowUnsafeUpdates = false;
                                }
                            }
                        }
                    }
                }
            }



  public static string GetFlexiTaskOutcomeId(Microsoft.SharePoint.Workflow.SPWorkflowTask task, string outcome)
            {
                if (task["MultiOutcomeTaskInfo"] == null)
                {
                    return string.Empty;
                }
                string xmlOutcome = HttpUtility.HtmlDecode(task["MultiOutcomeTaskInfo"].ToString());
                if (string.IsNullOrEmpty(xmlOutcome))
                {
                    return string.Empty;
                }
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xmlOutcome);
                var node = doc.SelectSingleNode(string.Format("/MultiOutcomeResponseInfo/AvailableOutcomes/ConfiguredOutcome[@Name='{0}']", outcome));
                return node.Attributes["Id"].Value;
            }
 public static bool AlterTask(SPListItem task, Hashtable htData, bool fSynchronous, int attempts, int milisecondsTimeout)
        {
            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(milisecondsTimeout);
                    }
                }
            }

            var result = SPWorkflowTask.AlterTask(task, htData, fSynchronous);
            return result;
        }

Upvotes: 1

gdbdable
gdbdable

Reputation: 4501

Did you try this code: (try-cacth block with redirection does the trick)

\\set to actual outcome id here, for ex. from OutComePanel control
taskItem[Nintex.Workflow.Common.NWSharePointObjects.FieldDecision] = 0; 

taskItem[Nintex.Workflow.Common.NWSharePointObjects.FieldComments] = " Some Comments";
taskItem.Update();
try
{
   Nintex.Workflow.Utility.RedirectOrCloseDialog(HttpContext.Current, Web.Url);
}
catch
{
}

?

Upvotes: 1

Related Questions