How To Update "Workflow Tasks" Using C#

In one of my projects, I want to update OOB "Workflow Tasks" list items using C#.

We have one visual web part which is used to show "view form" based on request details submitted by the end user. When approver logs into the system he/she is able to Approve/Reject the Request. When Approver clicks on Approve button then the status of the request is changed to Approve, and on click of approve button, we need to end current task and create a new task for another user to take further action. When one specific task gets completed then Nintex will automatically create new task for another user defined in the workflow.

We have used Nintex to send the notifications.

We have Approve button as below and click event accordingly.
  1. <asp:Button ID="btnApprove" CssClass="btn btn-primary" runat="server" Text="Approve" OnClick="btnApprove_Click" />  
On click of Approve button I have written code to update "Workflow Tasks" list.

We have unique Request ID and based on that we have added/updated item into the list. So for the latest task, we are using the ID in descending order as SharePoint creates a new ID for every new item addition. In CAML Query I have also matched "AssignedTo" field to get the task of that particular user.
  1. // reqID is the unique ID of that request getting from URL.  
  2. protected void btnApprove_Click(object sender, EventArgs e) {  
  3.     try {  
  4.         SPSite site = new SPSite(SPContext.Current.Web.Url);  
  5.         using(SPWeb web = site.OpenWeb()) {  
  6.             SPList lst = web.Lists.TryGetList("Workflow Tasks");  
  7.             string cQuery = "<Where><And><Eq><FieldRef Name='Title' /><Value Type='Text'>" + reqID + "</Value></Eq><Eq><FieldRef Name='AssignedTo'></FieldRef><Value Type='Integer'><UserID /></Value></Eq></And></Where><OrderBy><FieldRef Name='ID' Ascending='FALSE' /></OrderBy>";  
  8.             SPListItemCollection coll = getListItems(web, cQuery, "Workflow Tasks");  
  9.             if (coll.Count > 0) {  
  10.                 Guid commentsFieldId = Nintex.Workflow.Common.NWSharePointObjects.FieldComments;  
  11.                 Guid decisionFieldId = Nintex.Workflow.Common.NWSharePointObjects.FieldDecision;  
  12.                 SPListItem item = coll[0];  
  13.                 if (btnApprove.Text == "Approve") {  
  14.                     item[SPBuiltInFieldId.WorkflowOutcome] = strApproved;  
  15.                     item[decisionFieldId] = 1;  
  16.                 } else {  
  17.                     item[SPBuiltInFieldId.WorkflowOutcome] = strRejected;  
  18.                     item[decisionFieldId] = 2;  
  19.                 }  
  20.                 item[SPBuiltInFieldId.Completed] = true;  
  21.                 item[SPBuiltInFieldId.PercentComplete] = 1;  
  22.                 item[SPBuiltInFieldId.TaskStatus] = SPResource.GetString(new CultureInfo((int) web.Language, false), "WorkflowTaskStatusComplete"new object[0]);  
  23.                 item[commentsFieldId] = "Add your specific comment if you want to use later.";  
  24.                 site.AllowUnsafeUpdates = true;  
  25.                 web.AllowUnsafeUpdates = true;  
  26.                 item.Update();  
  27.                 web.AllowUnsafeUpdates = false;  
  28.                 site.AllowUnsafeUpdates = false;  
  29.             }  
  30.         }  
  31.     } catch (Exception ex) {  
  32.         //Catch exception  
  33.     }  
  34. }  
  35. public SPListItemCollection getListItems(SPWeb osweb, string cQuery, string lst) {  
  36.     SPListItemCollection coll = null;  
  37.     try {  
  38.         SPQuery oQuery = new SPQuery();  
  39.         SPList list = osweb.Lists.TryGetList(lst);  
  40.         oQuery.Query = cQuery;  
  41.         coll = list.GetItems(oQuery);  
  42.     } catch (Exception) {  
  43.         //Catch exception  
  44.     }  
  45.     return coll;  
We can also generalize above code for different buttons such as Resolve/Reopen Ok/Cancel depending on our project requirement.

Thanks.
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now