Friday 25 April 2014

Custom approve reject with nintex workflow webservice

This blog post will demonstrate how nintex workflow task can be assigned through custom code and how approve or reject call interacts with nintex through c#.

First we need to add a web reference to our project.

http://ServerName/_vti_bin/NintexWorkflow/Workflow.asmx

Lets name it NintexApprovalService .

Now on approve button click write the following code:

  try
            {
                string url = string.Empty;

                ProcessTaskResponseResult? result = null;
                url = service.GetItemURLBySomeCondition(your parameter here); //  write a function which will return web.url + "/"+ splistitem.url based on condition.
               NintexApprovalService.NintexWorkflowWS objNintextClient = new NintexApprovalService.NintexWorkflowWS();
               objNintextClient.UseDefaultCredentials = true;
               NintexApprovalService.UserTask[] userTask =                     objNintextClient.GetRunningWorkflowTasksForCurrentUser(url);    //This webmethod will give array of assigned tasks.

              foreach (NintexApprovalService.UserTask objTask in userTask)
                    {
                        int taskID = objTask.SharePointTaskId;
                        result = CallProcessTaskResponse(taskID);
                    } 


             }
catch(Exception e)
{
Logger.write(e.message + "--" + e.stackTrace);
}



 private NintexApprovalService.ProcessTaskResponseResult CallProcessTaskResponse(int TaskId)
 {

    NintexApprovalService.ProcessTaskResponseResult? obj= null;
    string taskList = "Workflow Tasks";
           {
                               NintexApprovalService.NintexWorkflowWS obj = new
NintexApprovalService.NintexWorkflowWS();
                obj.UseDefaultCredentials = true;             
                ob = obj.ProcessFlexiTaskResponse2(approvalComments, "Approve", TaskId,
taskList);
            }
            return ob.Value;

  }


Likely we can write a function for rejection case which will be called in reject button click.
Just we need to pass Reject in second parameter to ProcessFlexiTaskResponse2() method.

We can also check whether task is already assigned by this way:

 NintexApprovalService.UserTask[] userTask = objNintextClient.GetRunningWorkflowTasksForCurrentUser(url);
                        if (userTask != null && userTask.Length > 0)
                        {
                           //Write logic accordingly.
                        }