1

I have a workflow with Start Option "Start workflow automatically when an item is changed" and I have code that execute SPListItem.Update(), but this update doesn't force workflow to run execution. Why it doesn't work and how can I workaround this issue?

UPDATE: The workflow was created by SharePoint Designer and I don't use any Workflow functionality in my code, I just change values of SPListItems and update them.

6
  • Let us see your code, as well as what context (event receiver? etc). Commented Dec 27, 2011 at 14:33
  • What user account are you running as? Try running as a non-Administrator account and see if your workflows fire. Commented Dec 27, 2011 at 21:36
  • is your timer service running? Commented Feb 26, 2012 at 0:55
  • does your workflow run if you manually update the list item? Commented Feb 26, 2012 at 5:06
  • @David Lozzi Yes, service is running, it was the first thing that I checked with this issue. Commented Feb 26, 2012 at 10:21

3 Answers 3

1

please pay attention to the Task List. The Task lists execute the Workflow really. If you don't finalize the assigned task, your WF not run. This is my code to exceute a task and make te WF continue by the steps:

This code it's used ina SP WebPart, with a WorkFlow with two approvers.:

 SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite site = new SPSite(url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    try
                    {
                        SPList task = web.Lists["Tasks"];
                        SPListItem item = task.Items.GetItemById(Convert.ToInt32(Request.Params["ID"]));
                        if (item["WorkflowListId"] != null)
                        {
                            Guid sourceListID = new Guid(item["WorkflowListId"].ToString());
                            SPList sourceList = web.Lists.GetList(sourceListID, true);
                            int sourceListItemID = Convert.ToInt32(item["WorkflowItemId"]);
                            SPListItem sourceListItem = sourceList.GetItemById(sourceListItemID);

                         if (sourceListItem["Aprobador"].ToString() == "Aprobador 1" && Convert.ToString(sourceListItem["State"]) == "En Revisión")
                            {

                           sourceListItem["State"] = "Approved 1";
                                sourceListItem["Approver"] = "Approver 1";
                                sourceListItem.Update();

                                //Get the workflow instance id from Task item
                                Guid taskWorkflowInstanceID = new Guid(item["WorkflowInstanceID"].ToString());
                                SPWorkflow workflow = item.Workflows[taskWorkflowInstanceID];
                                SPWorkflowTask wfTask = workflow.Tasks[item.UniqueId];
                                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)wfTask.Web.Language, false), Strings.WorkflowStatusInProgress, new object[0]);
                                ht[SPBuiltInFieldId.WorkflowOutcome] = "Approved";
                                ht["TaskStatus"] = "Approved";
                                ht["FormData"] = SPWorkflowStatus.InProgress;


                                SPWorkflowTask.AlterTask((wfTask as SPListItem), ht, true);
                                Response.Redirect(Request.Params["Source"]);
                            }
                            else
                            {
                                if (sourceListItem["Approver"].ToString() == "Aprobador 1" && Convert.ToString(sourceListItem["State"]) == "Approved 1")
                                {
                                    sourceListItem["State"] = "Approved 2";
                                    sourceListItem["Aprobador"] = "Approver 2";
                                    sourceListItem.Update();

                                    //Get the workflow instance id from Task item
                                    Guid taskWorkflowInstanceID = new Guid(item["WorkflowInstanceID"].ToString());
                                    SPWorkflow workflow = item.Workflows[taskWorkflowInstanceID];
                                    SPWorkflowTask wfTask = workflow.Tasks[item.UniqueId];
                                    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)wfTask.Web.Language, false), Strings.WorkflowStatusInProgress, new object[0]);
                                    ht[SPBuiltInFieldId.WorkflowOutcome] = "Approved";
                                    ht["TaskStatus"] = "Approved";
                                    ht["FormData"] = SPWorkflowStatus.InProgress;

                                    SPWorkflowTask.AlterTask((wfTask as SPListItem), ht, true);
                                    Response.Redirect(Request.Params["Source"]);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                    finally
                    {
                        web.AllowUnsafeUpdates = false;
                    }
                }
            }
        });

The list with attached WF have a two fields: Approver and State. But the code that run the Task and make advance the WF it's:

 SPList task = web.Lists["Tasks"];
 SPListItem item = task.Items.GetItemById(Convert.ToInt32(Request.Params["ID"]));

 Guid taskWorkflowInstanceID = new Guid(item["WorkflowInstanceID"].ToString());
 SPWorkflow workflow = item.Workflows[taskWorkflowInstanceID];
 SPWorkflowTask wfTask = workflow.Tasks[item.UniqueId];
 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)wfTask.Web.Language, false), Strings.WorkflowStatusInProgress, new object[0]);
  ht[SPBuiltInFieldId.WorkflowOutcome] = "Approved";
  ht["TaskStatus"] = "Approved";
  ht["FormData"] = SPWorkflowStatus.InProgress;

  SPWorkflowTask.AlterTask((wfTask as SPListItem), ht, true);
2
  • You see, i don't use any Workflow functionality in my code. The workflow was created with SharePoint Designer and for some reason it isn't start when my code execute update. Commented Dec 27, 2011 at 14:26
  • Hi exactly, my code it's for a SP Designer WF, not WorkFlow Foundation, see you!! Commented Dec 27, 2011 at 14:46
0

Have you associate that workflow with a list yet? see this and this link to associate your workflow with list/library.

And if you have associate your workflow with list/library but still not running, try to change your workflow behavior ie add log to history activity or send email activity to debug your workflow on runtime.

Also its a good idea to post some of your code (where do you write that splistitem.update thing) and explain more clearly to forum.

0

If you are updating List item using SPSecurity.RunWithElevatedPrivileges(delegate() statement, you have to use below SPSite constructor for creating object of SPSite. using(SPSite site = new SPSite(SiteURL, UserToken))

For, fetching UserToken you may use below statement:

var userToken = SPContext.Current.Web.AllUsers["domain\\user"].UserToken;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.