Workflow stops responding after List item is updated programmatically
I ran into an interesting situation today, and I could not find a solution online, so I figured I’d post this up in case someone else runs into the same issue. I don’t guarantee that this is the best solution, so if anyone has any better ways to go about this, please comment.
I am creating a workflow that needs escalation capabilities. Because of the issues that SharePoint has with Delaying, I decided to make a separate console application that can be run as a timer job to check the running workflows on my list items and nudge them along if necessary.
That’s a bit of a broad, general overview. Here are the specific details of the issue I ran into:
The workflow is designed to wait until the item on which it is running has a certain field updated to a value other than the default. This is taken care of by placing an OnWorkflowItemChanged activity within a While activity:
If I edit the item myself, through the UI, it works without a hitch – once the correct field is edited, the workflow moves on. The interesting part is the behavior when the list item is edited programmatically in my console application:
workflow.ParentItem["FieldName"] = "NewValue";
workflow.ParentItem.Update();
The field will update, but the OnWorkflowItemChanged (ItemChaanged2) event won’t fire. In fact, after running the code above, the event won’t even fire from the UI anymore.
The solution?
Web services:
// Create the web service reference
Lists.Lists listsService = new Lists.Lists();
// Set credentials
listsService.Credentials = CredentialCache.DefaultCredentials;
// Set the URL
listsService.Url = string.Format(
"{0}/_vti_bin/lists.asmx",
siteURL
);
// Build the CAML statement
string updateCAML = "<Method ID='1' Cmd='Update'>";
updateCAML += "<Field Name='ID'>" + itemID.ToString() + "</Field>"; // ID of the item (index of the item in the list)
updateCAML += "<Field Name='FieldName'>NewValue</Field>"; // Update information
updateCAML += "</Method>";
// Build the XmlElement object that contains the update request
XmlDocument document = new XmlDocument();
XmlElement batch = document.CreateElement("Batch");
batch.SetAttribute("OnError", "Continue");
batch.SetAttribute("ListVersion", listVersion.ToString());
batch.InnerXml = updateCAML;
// Do the update
XmlNode returnNode = listsService.UpdateListItems(listID.ToString(), batch);
Like I said, I’m not sure if this is the best approach, but the workflow responded just fine when the update was made through a web service call instead of through the object model.
If you are trying to modify promoted fields from an InfoPath form, do not use the above method. To get the above to work, you would need to allow your promoted fields to be editable through the browser – and the edits would then not be available when you load the form again. If you are working with promoted fields, the following code will let you alter the field in the form XML itself, which will both solve the above problem and still keep within the spirit of a Forms Library:
private void AlterField(SPListItem item, string fieldXPath, string newValue, string xmlNamespace, string xmlNamespaceUri)
{
SPFile file = item.File;
XmlDocument form = new XmlDocument();
using (StreamReader reader = new StreamReader(file.OpenBinaryStream()))
{
form.LoadXml(reader.ReadToEnd());
}
MemoryStream inStream = new MemoryStream(file.OpenBinary());
XmlDocument myDoc = new XmlDocument();
myDoc.PreserveWhitespace = true;
myDoc.Load(inStream);
XmlElement root = myDoc.DocumentElement;
XPathNavigator navigator = form.CreateNavigator();
XmlNamespaceManager manager = new XmlNamespaceManager(navigator.NameTable);
manager.AddNamespace(xmlNamespace, xmlNameSpaceUri);
XmlNode fieldToAlter = root.SelectSingleNode(
fieldXPath,
manager
);
fieldToAlter.InnerText = newValue;
MemoryStream outStream = new MemoryStream();
myDoc.Save(outStream);
file.SaveBinary(outStream.ToArray());
}
Thanks to Christopher Jones for this solution: http://www.qualitysharepoint.com/2009/03/update-infopath-form-from-workflow.html.

Hi Kevin,
I have the same problem! Thanks god I found this post.
Let me know if you had another solution to solve it!
Thanks
Andre Rentes
January 31, 2010 at 11:03 pm
Since I was promoting the properties from an InfoPath form, the second solution is the one I went with, and it seems to be a good one. I didn’t find another solution to this problem – once I found the one Christopher Jones posted, I was happy with what I had.
Kevin Akselrod
April 13, 2010 at 2:04 am