Tuesday, October 11, 2016

Approve/Reject Workflow Task Programatically

If you doing same using event receivers, note that don't disable event firing.


Before calling this funciton, make sure that event firing is ENABLED. That’s because SPWorkflowTask.AlterTask() needs to fire events……

Use below C# code to Approve Task Item programmatically

Question
Sign in to vote
int _itemId = 1;

            using (SPSite _siite = new SPSite(SPContext.Current.Site.Url))
            {
                using (SPWeb _web = _site.OpenWeb())
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        _site.AllowUnsafeUpdates = true;
                        SPList _taskList = _web.Lists["Tasks"];
                        SPListItem _taskItem = _taskList.GetItemById(_itemId);

                        if (_taskItem != null)
                        {
                            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)_taskItem.Web.Language, false), Strings.WorkflowStatusCompleted, new object[0]);
                      
                            ht[SPBuiltInFieldId.WorkflowOutcome] = "Approved";
                            ht["TaskStatus"] = "Approved";             
                            ht["FormData"] = SPWorkflowStatus.Completed;
                            ht["ows_FieldName_Comments"] = "Approver comments will be here";

                            _web.AllowUnsafeUpdates = true;
                            bool alterstatus = true;
                            while (!(alterstatus = SPWorkflowTask.AlterTask(_taskItem, ht, true)))
                            {
                                System.Threading.Thread.Sleep(1000);
                                alterstatus = SPWorkflowTask.AlterTask(_taskItem, ht, true);
                            }
                           
                            _web.AllowUnsafeUpdates = false;
                        }
                    });
                }
            }

ht["TaskStatus"] = "Approved";attribute that solved my problem


To Reject Task Item Replace below lines in above code.....

          ht[SPBuiltInFieldId.WorkflowOutcome] = "Rejected";
          ht["TaskStatus"] = "Rejected";

No comments:

Post a Comment

Image noise comparison methods

 1. using reference image technique     - peak_signal_noise_ratio (PSNR)     - SSI 2. non-reference image technique     - BRISQUE python pac...