Seems like some weird race condition. I was able to partially reproduce your problem with some sample code:
using (var client = new ClientContext(ConfigurationManager.AppSettings["Site"]))
{
client.Load(client.Site);
client.Load(client.Site.RootWeb);
var list = client.Site.RootWeb.Lists.GetByTitle("Shared Documents");
client.Load(list);
client.Load(list.RootFolder);
client.ExecuteQuery();
var fci = new FileCreationInformation()
{
Content = File.ReadAllBytes(ConfigurationManager.AppSettings["File"]),
Overwrite = true,
Url = list.RootFolder.ServerRelativeUrl + "/" + "file.xml"
};
var file = list.RootFolder.Files.Add(fci);
client.ExecuteQuery();
//If following 3 lines are commented out, problem you described may or may not occur. I wasn't able to run into problems, when i loaded file and list item before.
//client.Load(file);
//client.Load(file.ListItemAllFields);
//client.ExecuteQuery();
//Problem also doesn't occur, when i put here
//System.Threading.Thread.Sleep(1000);
//If code just runs here without delay, sometimes i run into issue you described.
var itm = file.ListItemAllFields;
itm["SampleColumn"] = "Test content!";
itm.Update();
client.ExecuteQuery();
}
and workflow:
public Workflow1()
{
InitializeComponent();
}
public Guid workflowId = default(System.Guid);
public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties();
private void onWorkflowActivated1_Invoked(object sender, ExternalDataEventArgs e)
{
var comment = "Workflow started!, item SampleColumn is: " +
workflowProperties.Item["SampleColumn"];
SPWorkflow.CreateHistoryEvent(workflowProperties.Web, WorkflowInstanceId, 0, workflowProperties.Web.CurrentUser, new TimeSpan(), "Update", comment, string.Empty);
}
private void onWorkflowItemChanged1_Invoked(object sender, ExternalDataEventArgs e)
{
var comment = "Workflow continous!, item Title is: " + workflowProperties.Item["SampleColumn"];
SPWorkflow.CreateHistoryEvent(workflowProperties.Web, WorkflowInstanceId, 0, workflowProperties.Web.CurrentUser, new TimeSpan(), "Update", comment, string.Empty);
}
And workflow itself
If you had something different, please let me know. I had myself some problems with declarative workflows which were running on SharePoint library and most of the time i was not able to find nice solution.