/// <summary> /// Gets the workflow status string. /// </summary> /// <param name="itemInfo">The item info.</param> /// <param name="web">The web.</param> /// <returns>status string</returns> private string GetWorkflowStatusString(WorkflowItemInfo itemInfo, SPWeb web) { string output = string.Empty; switch (itemInfo.Status.ToLower()) { case "in progress": output = ResourcesHelper.GetLocalizedString("workflow_approvaltaskstatus_inprogress", web); break; case "cancelled": output = ResourcesHelper.GetLocalizedString("workflow_approvaltaskstatus_cancelled", web); break; case "completed": output = ResourcesHelper.GetLocalizedString("workflow_approvaltaskstatus_completed", web); break; } if (string.IsNullOrEmpty(output)) { output = ResourcesHelper.GetLocalizedString("workflow_approvaltaskstatus_completed", web); } return(output); }
/// <summary> /// Updates the item workflow status. /// </summary> /// <param name="itemInfo">The item info.</param> /// <returns>update status</returns> public bool UpdateItemWorkflowStatus(WorkflowItemInfo itemInfo) { var result = false; Guid siteId = SPContext.Current.Site.ID; Guid webId = SPContext.Current.Web.ID; SPSecurity.RunWithElevatedPrivileges(delegate { using (SPSite site = new SPSite(siteId)) { using (SPWeb web = site.OpenWeb(webId)) { bool allowUnsafeUpdates = web.AllowUnsafeUpdates; try { var list = web.Lists[itemInfo.ListId]; if (list != null) { var item = list.GetItemById(itemInfo.ItemId); if (item != null) { string fieldName = ResourcesHelper.GetLocalizedString("workflow_approval_instancename", web); if (item.Fields.ContainsField(fieldName)) { web.AllowUnsafeUpdates = true; string status = this.GetWorkflowStatusString(itemInfo, web); string statusLink = string.Format("{0}/_layouts/15/wrkstat.aspx?List={1}&WorkflowInstanceName={2}, {3}", web.Url, itemInfo.ListId, itemInfo.WorkflowInstanceId, status); item[fieldName] = statusLink; item.SystemUpdate(); if (itemInfo.Status.Equals("Cancelled", StringComparison.OrdinalIgnoreCase) && item.Tasks != null) { foreach (SPWorkflowTask taskItem in item.Tasks) { taskItem["Status"] = "Canceled"; taskItem.SystemUpdate(); } } result = true; } } } } catch (Exception ex) { throw; } finally { web.AllowUnsafeUpdates = allowUnsafeUpdates; } } } }); return(result); }