12.05.2011 – Anton Naumenko

Fix for Initiation Form generated by Visual Studio 2010 for workflows associated to Content Types

There is a wizard that generates initiation and association forms for workflows in Visual Studio 2010. The problem is that initiation form can start only list workflows and site workflows. In the case of content type workflow, generated code does not work. Here is originally generated code:

private void StartListWorkflow()
{
SPWorkflowAssociation association = this.workflowList.WorkflowAssociations[new Guid(this.associationGuid)];
this.Web.Site.WorkflowManager.StartWorkflow(workflowListItem, association, GetInitiationData());
SPUtility.Redirect(this.workflowList.DefaultViewUrl, SPRedirectFlags.UseSource, HttpContext.Current);
}

This happens because content type workflow is associated to the list's contenttype. Generated code tries to get it from the list associations, i.e. this.workflowList.WorkflowAssociations. Here is a simple fix for this:

private void StartListWorkflow()
{
SPWorkflowAssociation association = this.workflowList.WorkflowAssociations[new Guid(this.associationGuid)];
if (association == null)//content type workflow
{
association = this.workflowListItem.ContentType.WorkflowAssociations[new Guid(this.associationGuid)];
}
this.Web.Site.WorkflowManager.StartWorkflow(workflowListItem, association, GetInitiationData());
SPUtility.Redirect(this.workflowList.DefaultViewUrl, SPRedirectFlags.UseSource, HttpContext.Current);
}

If you do not get workflow association from the list, then try to get it from the item's contenttype.

Inno-W mark
Anton Naumenko
Anton Naumenko

Posts by Anton