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.
- <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.
-
- protected void btnApprove_Click(object sender, EventArgs e) {
- try {
- SPSite site = new SPSite(SPContext.Current.Web.Url);
- using(SPWeb web = site.OpenWeb()) {
- SPList lst = web.Lists.TryGetList("Workflow Tasks");
- 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>";
- SPListItemCollection coll = getListItems(web, cQuery, "Workflow Tasks");
- if (coll.Count > 0) {
- Guid commentsFieldId = Nintex.Workflow.Common.NWSharePointObjects.FieldComments;
- Guid decisionFieldId = Nintex.Workflow.Common.NWSharePointObjects.FieldDecision;
- SPListItem item = coll[0];
- if (btnApprove.Text == "Approve") {
- item[SPBuiltInFieldId.WorkflowOutcome] = strApproved;
- item[decisionFieldId] = 1;
- } else {
- item[SPBuiltInFieldId.WorkflowOutcome] = strRejected;
- item[decisionFieldId] = 2;
- }
- item[SPBuiltInFieldId.Completed] = true;
- item[SPBuiltInFieldId.PercentComplete] = 1;
- item[SPBuiltInFieldId.TaskStatus] = SPResource.GetString(new CultureInfo((int) web.Language, false), "WorkflowTaskStatusComplete", new object[0]);
- item[commentsFieldId] = "Add your specific comment if you want to use later.";
- site.AllowUnsafeUpdates = true;
- web.AllowUnsafeUpdates = true;
- item.Update();
- web.AllowUnsafeUpdates = false;
- site.AllowUnsafeUpdates = false;
- }
- }
- } catch (Exception ex) {
-
- }
- }
- public SPListItemCollection getListItems(SPWeb osweb, string cQuery, string lst) {
- SPListItemCollection coll = null;
- try {
- SPQuery oQuery = new SPQuery();
- SPList list = osweb.Lists.TryGetList(lst);
- oQuery.Query = cQuery;
- coll = list.GetItems(oQuery);
- } catch (Exception) {
-
- }
- return coll;
- }
We can also generalize above code for different buttons such as Resolve/Reopen Ok/Cancel depending on our project requirement.
Thanks.