none
workflow cannot detect changes in column from custom web part in editform.aspx

    Question

  • Hai. i have made a custom web part and already uploaded in editform.aspx. This customized web part will input every value or changes from that web part into TaskList column. But somehow when i change a column from that web part, let's say column "Document Status" for "Approved" or "Rejected" it cannot detect the changes in the workflow. This is the code to check the changes in column "Document Status".

          private void ontaskchanged_invoked(object sender, ExternalDataEventArgs e)
            {
                onTaskChanged2_AfterProperties1 = onTaskChanged2.AfterProperties;
                onTaskChanged2_BeforeProperties1 = onTaskChanged2.BeforeProperties;
            }

            //to check if checker approve or reject
            private void docapproval(object sender, ConditionalEventArgs e)
            {
                Guid docStatusField = workflowProperties.TaskList.Fields["Document Status"].Id;

                if (workflowProperties.TaskList.Fields[docStatusField].ToString() == "Approved")
                {
                    e.Result = true;
                }
                else if (workflowProperties.TaskList.Fields[docStatusField].ToString() == "Rejected")
                {
                    e.Result = false;
                }

            }

    If i use this code in default editform.aspx it can detect the changes either it is approved or rejected. Is there a solution on how to make sure that every changes from custom web part can be detect by workflow? Examples will be much appreciated. Thanks in advance.


    when you know better, you do better

    Wednesday, January 23, 2013 3:36 AM

Answers

  • Hi Amirul ,

    I am not very sure the way you are updating the TaskList column . I mean what is the context with which you are trying to update the TaskList Column (i.e runwithelivated privillege or current site context. ).

    Below are the piece of code that works for me :

       

                                  

            /// </summary>/// To Update the list item 

            /// </summary>
            /// <param name="Approveed"></param>
            /// <param name="listId"></param>
            /// <param name="id"></param>
            protected void UpdateListItem(bool Approveed, string listId, int id)
            {

                Guid g = new Guid(listId);
                siteContext = SPContext.Current.Site;
                SPWeb webContext = siteContext.OpenWeb();
                strSiteUrl = siteContext.Url;
                string outcomeMessage = string.Empty;

                foreach (SPWeb web in siteContext.AllWebs)
                {

                    if (ListExists(web, listId))
                    {
                        spList = web.Lists[g];
                        break;

                    }
                }


                try
                {

                    if (spList != null)
                    {
                        SPListItem item = spList.GetItemById(id);

                        if (Approveed)
                        {
                            item["WorkflowState"] = "Approval - Accepted";
                            outcomeMessage = "Approval - Accepted";
                        }
                        else
                        {
                            item["WorkflowState"] = "Approval - Rejected";
                            outcomeMessage = "Approval - Rejected";
                        }
                        item["Approver"] = webContext.CurrentUser.Name;
                        item["Approved Date"] = System.DateTime.Now.ToString();
                        item["approverComments"] = txtComments.Text;
                        webContext.AllowUnsafeUpdates = true;
                        item.UpdateOverwriteVersion();                 
                        UpdateTasks(item, webContext, outcomeMessage);// to udate the tasks
                        webContext.AllowUnsafeUpdates = false;

                    }
                }
                catch (SPException exc)
                {
                    //TODO:// EXCEPTION HANDLING
                }
                catch (Exception exc)
                {
                    //TODO:// EXCEPTION HANDLING
                }

            }
            /// <summary>
            /// To Update the Tasks
            /// </summary>
            /// <param name="item"></param>
            /// <param name="webContext"></param>
            public void UpdateTasks(SPListItem item, SPWeb webContext,string outcomeMessage)
            {
                SPWorkflowTask taskedit = null;
                foreach (SPWorkflowTask task in item.Tasks)
                {
                    taskedit = task;
                    if (task["Status"].ToString() != "Completed")
                    {
                        // alter the task

                        /*  task["Completed"] = true;
                        task["Status"] = "Completed";
                        task.Update();
                        */
                        // alter the task
                        Hashtable ht = new Hashtable();
                       // ht["Status"] = txtComments.Text;
                        ht["Status"] = "Completed";
                        ht["Comments"] = txtComments.Text;
                        ht[SPBuiltInFieldId.Completed] = "TRUE";
                        ht[SPBuiltInFieldId.PercentComplete] = 1;

                        ht[SPBuiltInFieldId.Outcome] = outcomeMessage;
                       ht["Outcome"] = outcomeMessage;
                       ht["WorkflowOutcome"] = outcomeMessage;

                        SPWorkflowTask.AlterTask((taskedit as SPListItem), ht, true);
                        webContext.Update();
                    }

                }

            }


    Md Athar Raza Faridi,Programmer Anaylst [MCTS],.NET AND Sharepoint and MOSS2007 Developer.


    Wednesday, January 23, 2013 4:50 AM

All replies

  • Hi Amirul ,

    I am not very sure the way you are updating the TaskList column . I mean what is the context with which you are trying to update the TaskList Column (i.e runwithelivated privillege or current site context. ).

    Below are the piece of code that works for me :

       

                                  

            /// </summary>/// To Update the list item 

            /// </summary>
            /// <param name="Approveed"></param>
            /// <param name="listId"></param>
            /// <param name="id"></param>
            protected void UpdateListItem(bool Approveed, string listId, int id)
            {

                Guid g = new Guid(listId);
                siteContext = SPContext.Current.Site;
                SPWeb webContext = siteContext.OpenWeb();
                strSiteUrl = siteContext.Url;
                string outcomeMessage = string.Empty;

                foreach (SPWeb web in siteContext.AllWebs)
                {

                    if (ListExists(web, listId))
                    {
                        spList = web.Lists[g];
                        break;

                    }
                }


                try
                {

                    if (spList != null)
                    {
                        SPListItem item = spList.GetItemById(id);

                        if (Approveed)
                        {
                            item["WorkflowState"] = "Approval - Accepted";
                            outcomeMessage = "Approval - Accepted";
                        }
                        else
                        {
                            item["WorkflowState"] = "Approval - Rejected";
                            outcomeMessage = "Approval - Rejected";
                        }
                        item["Approver"] = webContext.CurrentUser.Name;
                        item["Approved Date"] = System.DateTime.Now.ToString();
                        item["approverComments"] = txtComments.Text;
                        webContext.AllowUnsafeUpdates = true;
                        item.UpdateOverwriteVersion();                 
                        UpdateTasks(item, webContext, outcomeMessage);// to udate the tasks
                        webContext.AllowUnsafeUpdates = false;

                    }
                }
                catch (SPException exc)
                {
                    //TODO:// EXCEPTION HANDLING
                }
                catch (Exception exc)
                {
                    //TODO:// EXCEPTION HANDLING
                }

            }
            /// <summary>
            /// To Update the Tasks
            /// </summary>
            /// <param name="item"></param>
            /// <param name="webContext"></param>
            public void UpdateTasks(SPListItem item, SPWeb webContext,string outcomeMessage)
            {
                SPWorkflowTask taskedit = null;
                foreach (SPWorkflowTask task in item.Tasks)
                {
                    taskedit = task;
                    if (task["Status"].ToString() != "Completed")
                    {
                        // alter the task

                        /*  task["Completed"] = true;
                        task["Status"] = "Completed";
                        task.Update();
                        */
                        // alter the task
                        Hashtable ht = new Hashtable();
                       // ht["Status"] = txtComments.Text;
                        ht["Status"] = "Completed";
                        ht["Comments"] = txtComments.Text;
                        ht[SPBuiltInFieldId.Completed] = "TRUE";
                        ht[SPBuiltInFieldId.PercentComplete] = 1;

                        ht[SPBuiltInFieldId.Outcome] = outcomeMessage;
                       ht["Outcome"] = outcomeMessage;
                       ht["WorkflowOutcome"] = outcomeMessage;

                        SPWorkflowTask.AlterTask((taskedit as SPListItem), ht, true);
                        webContext.Update();
                    }

                }

            }


    Md Athar Raza Faridi,Programmer Anaylst [MCTS],.NET AND Sharepoint and MOSS2007 Developer.


    Wednesday, January 23, 2013 4:50 AM
  • Hi Athar. Thanks for the reply. I have finally found the cause of the problem. How silly of me not to add "Document Status" column in workflow task in list settings. Maybe thats the problem it cannot detect the value of that column. Thanks again


    when you know better, you do better

    Wednesday, January 23, 2013 8:30 AM