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.
                        }


9 comments:

  1. Hi, I am trying to customize the approve reject through web services and i have an exception. Do you encounter the same problem?
    The below are my code:

    NintexWS.UserTask[] usertask = client.GetRunningWorkflowTasksForListItem(105, "Borrow Applicant");
    client.ProcessFlexiTaskResponse2("FlexiTask", "Approve", usertask[0].SharePointTaskId, "Borrow Applicant");

    Exception:

    System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> The associated Workflow Task item does not have the required human workflow id column at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall) at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters) at DSIWebpart.NintexWS.NintexWorkflowWS.ProcessFlexiTaskResponse2(String comments, String outcome, Int32 spTaskId, String taskListName) at DSIWebpart.WPApprovalPage.WPApprovalPage.btnApprove_Click(Object sender, EventArgs e)

    ReplyDelete
    Replies
    1. and my Site is sharepoint 2013 and nintex 2013. any difference in the web service?

      Delete
  2. i can't able to use webservice to approve or reject it throws error(can't able to find running tasks)..can you share your soln for my referene..

    ReplyDelete
  3. I am getting the following error: http status 401: Unauthorized
    If I use SPSecurity.RunWithElevatedPrivileges, I get Invalid User


    Kindly i need solution for this issue


    ReplyDelete
    Replies
    1. I am getting InvalidUser error too? Mohamad, did you manage to find a solution to the error?

      Delete
    2. This comment has been removed by the author.

      Delete
    3. Lina Naser, did you find the solution. I am in the same boat.

      Please help

      Delete
  4. Pls help i'm getting "item does not exist , item may have been deleted by another user" at the point of calling ProcessFlexiTaskResponse2 method

    ReplyDelete
  5. Hi I am getting an exception stating that Invalid User all time. even the current user has the task which is pending .
    In the above code i have changed the web method GetRunningWorkflowTasksForCurrentUser --> GetRunningWorkflowTasksForListItem because i am not getting the above method is working . please help me to solve this issues.

    Also if use --> objNintextClient.Credentials = new System.Net.NetworkCredential("","",""); it will update the task. I believe the UseDefaultCredentials is not working, please help me it's very urgent solution i need.

    ReplyDelete