views:

24

answers:

1

I am using SharePoint Foundation (Sharepoint 2010) to develop a workflow in Visual Studio with an ASP.NET Workflow Initiation Form.

I use this form to set some properties on the share point list item on which the workflow instance is being started. Sometimes I get an Error message in the brwoser window, something like:

Error 

Failed%20to%20Start%20Workflow 

Troubleshoot issues with Microsoft SharePoint Foundation. 

Correlation ID: 0b8e0b67-f824-4aa5-8316-424ada134f8d 

Date and Time: 6/25/2010 10:59:17 AM 

Go back to site

This behaviour seems intermittent. What's going on?

+1  A: 

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:

  1. Start the workflow so that the initiation form shows.
  2. Change some property on the WorkflowProperties.Item
  3. Call WorkflowProperties.Item.Update to save the changes to SharePoint
  4. 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.

Michael Rodrigues

related questions