Problem Details:
We have created a custom reusable workflow and added with a list on which a workflow (custom workflow) is to start automatically when the item is created. It works great when I add a new item to the list through the SharePoint UI it starts and completes as expected.
We want the list item to be added programmatically, using the SharePoint object model. And when I do this and then look at the list, the workflow hasn't started automatically.
Why?
My SharePoint Environment:
- SP 2010 Ent (Build Version 14.0.4762.1000)
- SPD 2010 Workflow
So, It can happen if yours Server Farm Version is
SharePoint Server 2010 RTM – Build Version 14.0.4762.1000 (my case)
or below
Build Version 14.0.6029.1000
Solution:
The problem can be solved easily if you upgrade the Server Farm to Build Version 14.0.6029.1000. because the same scenario is working for me in Another Server where Build Version is 14.0.6029.1000. It means this is a bug of Initial Product Version (Exactly I don’t know when it is solved after 14.0.6029.1000).
But if you are unable to Upgrade Farm due to some other dependency then Start Workflow Manually. I am having the same problem for adding List Item from custom web part. Workflow doesn't Auto-Start when i add new Item Pragmatically and I cannot Update Farm to Build Version 14.0.6029.1000 due to others dependency.
Initially I used the following code to start workflow pragmatically
private static void StartWorkflow(SPListItem listItem, string workflowName)
{
// Get the workflow by name that's associated with the list item
SPWorkflowAssociation wfAssoc = listItem.ParentList.WorkflowAssociations.GetAssociationByName(workflowName, System.Globalization.CultureInfo.CurrentCulture);
// Start the workflow
listItem.Web.Site.WorkflowManager.StartWorkflow(listItem, wfAssoc, wfAssoc.AssociationData, true);
listItem.Update();
}
But First time it works but from the next time it shows the following error:

How to solve:
Finally I have solved my problem in the following way:
I have change the code to Start Workflow which executes faster that the previous one
public void StartWorkflow(SPListItem listItem, SPSite spSite, string wfName)
{
SPList parentList = listItem.ParentList;
SPWorkflowAssociationCollection associationCollection = parentList.WorkflowAssociations;
foreach (SPWorkflowAssociation association in associationCollection)
{
if (association.Name == wfName)
{
association.AutoStartChange = true;
association.AutoStartCreate = false;
association.AssociationData = string.Empty;
spSite.WorkflowManager.StartWorkflow(listItem, association, association.AssociationData);
}
}
}
For more Details please read http://jainnitin2411.wordpress.com/2012/07/06/programmaticallystartsharepointworkflow/
Then i called it from Entry From "Save" Button instead of Event Receiver
currentWeb.AllowUnsafeUpdates = true;
call StartWorkflow(...);
//This has to be after start workflow otherwise you will face another Error "Save Conflict.\n\nYour changes conflict with those made concurrently by another user. If you want your changes to be applied, click Back in your Web browser, refresh the page, and resubmit your changes."
currentWeb.AllowUnsafeUpdates = false;
Hope this will help you!!
Related Error:
http://connect.nintex.com/forums/thread/16616.aspx
http://social.msdn.microsoft.com/Forums/bg-BG/sharepointdevelopmentprevious/thread/6bb56995-3a61-4095-aac8-9ca3a883d12c
http://social.technet.microsoft.com/forums/en-US/sharepointadminprevious/thread/83ae2900-6ed5-41d3-8682-73fc2ddea43f/
http://social.technet.microsoft.com/forums/en-US/sharepointcustomizationprevious/thread/fc4e1073-d67f-449a-b443-e5805f5358c7/
http://social.msdn.microsoft.com/forums/en-US/sharepointcustomizationlegacy/thread/724d1347-c1ab-4dce-96b4-a97a0a320b8b/