Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Complete Workflow after approving all tasks

$
0
0

Hello everybody,

I'm face to a big problem with approval workflow OOTB. In my case, when one user from approvers approve his task. The workflow must be completed and we we din't need to wait other approvment. So, I developed an event receiver on Tasks Workflow list and I checked the first approvment. After, that I approve all other tasks by code. The problem, the status of workflow is still equal to "In progress" despite the ending of all tasks ! It is strange.

This my code in event receiver of the list :

  public override void ItemUpdated(SPItemEventProperties properties)
        {
            base.ItemUpdated(properties);
            SPList list = properties.List;            
            SPWeb web = properties.Web;
            SPList createDocument = DMSUtility.GetListByUrl(web, DMSConstants.LIST_CreateDocument);
            string workflowItemId = string.Empty;
            string taskStatus = string.Empty;
            string taskID = string.Empty;
            string taskworkflowStatus = string.Empty;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(web.Site.ID))
                {
                    using (SPWeb webSecure = site.OpenWeb())
                    {
                        if (list.RootFolder.Url.Equals((DMSConstants.LIST_CreateDocumentWorkflowTasks)))
                        {

                            //Get Tasks item
                            SPListItem itemTask = properties.ListItem;

                            if (itemTask["WorkflowOutcome"] != null && itemTask["WorkflowItemId"] != null)
                            {
                                workflowItemId = itemTask["WorkflowItemId"].ToString();
                                taskStatus = itemTask["Status"].ToString();
                                taskworkflowStatus = itemTask["WorkflowOutcome"].ToString();
                                taskID = itemTask["ID"].ToString();
                                if (taskworkflowStatus.Equals("Approved"))
                                {

                                  
                                    SPListItem item = createDocument.GetItemById(Convert.ToInt16(workflowItemId));

                                   
                                    // fetch workflow tasks
                                    SPWorkflowTaskCollection wfTasks = item.Tasks;
                                   
                                    int i=0;
                                    foreach (SPWorkflowTask task in wfTasks)
                                    {
                                        // build a hashtable with the values to be changed to mark the task as complete
                                        if (task["ID"].ToString() != taskID && task["Status"].ToString()!="Completed")
                                        {
                                            using (DisabledItemEventsScope scope = new DisabledItemEventsScope())
                                            {
                                                task.Web.AllowUnsafeUpdates = true;

                                                task["Status"] = "Completed";
                                                task["WorkflowOutcome"] = "Approved";
                                                task[SPBuiltInFieldId.WorkflowVersion] = 1;
                                                task.SystemUpdate();

                                                Hashtable ht = new Hashtable();
                                                ht[SPBuiltInFieldId.Completed] = "TRUE";
                                                ht["Completed"] = "TRUE";
                                                ht[SPBuiltInFieldId.PercentComplete] = 1.0f;
                                                ht["PercentComplete"] = 1.0f;
                                                ht["Status"] = "Completed";
                                                ht[SPBuiltInFieldId.TaskStatus] = SPResource.GetString
                                                    (new CultureInfo((int)task.Web.Language, false),
                                                    Strings.WorkflowStatusCompleted, new object[0]);
                                                ht[SPBuiltInFieldId.WorkflowOutcome] = "Approved";
                                                ht["TaskStatus"] = "Approved";                                                                                                    
                                                ht["FormData"] = SPWorkflowStatus.Completed;
                                                // alter the task using a trick to prevent task lock issue
                                                //AlterTask((task as SPListItem), ht, true, 5, 100);
                                                SPWorkflowTask.AlterTask((task as SPListItem), ht, true);
                                                while (!(bool)item.Tasks[i]["Completed"])
                                                {
                                                         Thread.Sleep(1000);
                                                }
                                                task.Web.AllowUnsafeUpdates = false;
                                                task.Web.Update();
                                            }
                                        }
                                        i++;
                                    }
                                }

                            }
                        }
                    }
                }

            });
       }

Do you have any idea ?

Thanks.


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>