The problem is that the SharePoint workflow initiation form caches the workflow List Item on PageLoad, and when you make changes to the item between the page load and calling HandleStartWorkflow (i.e. clicking on the Start Workflow button), SharePoint freaks out that the item that you're talking about is different (the cached item) to the item that exists in Share Point (which incorporates the changes you've just made).
The general steps to reproduce the problem are:
- Start the workflow so that the initiation form shows.
- Change some property on the WorkflowProperties.Item
- Call WorkflowProperties.Item.Update to save the changes to SharePoint
- Call HandleStartWorkflow.
You will get the error.
The error may seem intermittent if some of the time the change to the workflow item properties is made in a separate page load 'session' to the session that the Start Workflow button is clicked. For example, if you PostBack some form data and then click the Start workflow button then everything works, because the PostBack happened and then the page was loaded.
But if you use an ASP TextBox OnTextChanged event, change the text and then click the Start Workflow button, the OnChanged event is fired AFTER the page is loaded with the old data, the data is changed to the new data and the workflow is started before the item data is re-loaded from SharePoint.
The fix is easy: Reload the workflow item data JUST before starting the workflow. This will cause your cached workflow item and the sharepoint version of the item to be the same, and share point will be happy.
In the "Workflow Initiation Code" region, change:
Private Sub StartListWorkflow()
Dim association As SPWorkflowAssociation = workflowList.WorkflowAssociations.Item(New Guid(associationGuid))
Web.Site.WorkflowManager.StartWorkflow(workflowListItem, association, GetInitiationData)
SPUtility.Redirect(workflowList.DefaultViewUrl, SPRedirectFlags.UseSource, System.Web.HttpContext.Current)
End Sub
to
Private Sub StartListWorkflow()
'Re-initialize the workflow parameters, particularly the workflowListItem, in case it has been changed since page load.'
InitializeParams()
Dim association As SPWorkflowAssociation = workflowList.WorkflowAssociations.Item(New Guid(associationGuid))
Web.Site.WorkflowManager.StartWorkflow(workflowListItem, association, GetInitiationData)
SPUtility.Redirect(workflowList.DefaultViewUrl, SPRedirectFlags.UseSource, System.Web.HttpContext.Current)
End Sub
And everything should start working again.