Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
    
Let us see your code, as well as what context (event receiver? etc). –  James Love Dec 27 '11 at 14:33
    
What user account are you running as? Try running as a non-Administrator account and see if your workflows fire. –  Chris Johnson Dec 27 '11 at 21:36
    
is your timer service running? –  David Lozzi Feb 26 '12 at 0:55
    
does your workflow run if you manually update the list item? –  justforkix09 Feb 26 '12 at 5:06
    
@David Lozzi Yes, service is running, it was the first thing that I checked with this issue. –  Dmitriy Inozemtsev Feb 26 '12 at 10:21
show 1 more comment

2 Answers

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);
share|improve this answer
    
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. –  Dmitriy Inozemtsev Dec 27 '11 at 14:26
    
Hi exactly, my code it's for a SP Designer WF, not WorkFlow Foundation, see you!! –  Andrés Dec 27 '11 at 14:46
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.