Wednesday, March 18, 2009

Getting Workflow Association from Content Type of Sharepoint Lists

Recently i have been working on Sharepoint Custom Workflows and Workflow Association with Sharepoint Lists and Content Type within the list. Thought of sharing some of the learnings from my experience.

Once the workflow is associated with Content Type/SPLists, its very common to update Workflow Instances.
As the workflow assembly keep on changing, Whenever you deploy new version of Workflow Assembly/Workflows Solution Package, SharePoint updates the workflow association to restrict new instance of the workflow. Even if the workflow class is not modified.
To over come this we need to get the workflow association object from List/Content type, depending on how we have associated the workflow.
If you want to update workflow association to allow new instances, you need to get a reference of Workflow Association Object and Enable it.
1. Getting Workflow Association Object from Content T(when workflow is associated with content type of Sharepoint List)

private SPWorkflowAssociation GetAnnualApprovalWFAssociation(SPContentType contentType)
{
SPWorkflowAssociation resultWFAssociation= null;
//Loop through all the workflow associations and return the correct one
foreach (SPWorkflowAssociation wfAssociation in contentType.WorkflowAssociations)
{
if(wfAssociation.BaseId == Constants.workflowGuid)
resultWFAssociation= wfAssociation;
}
return resultWFAssociation;
}
}
Note: SPWorkflowAssociations collection class defines two methods that can be used to get the workflow associations
  • GetAssociationByBaseID(wfGUID)
  • GetAssociationByName(name,cultureInfo)

This methods will return value only when the WorkflowAssociations are not restricted to allow new instances. Hence you can not completely trust this methods. This is why i have used a work around which loops through all the workflow associations (including deactive one) and compare the workflow association with Workflow GUID.

2. Getting workflow association with Sharepiont List

> SPList object contains a collection called WorkflowAssociations which is same as content type's workflow association object, so you can replace SPContentType object with SPList object and the above method will work.

Enabling WorkflowAssociation to AllowNewInstance

Once you have reference of SPWorfklowAssociation object, you can just set it's enabled property to true and invoke UpdateWorkflowAssociation(SPWorkflowAssociation) method on Content Type or SPLIst object.

SPWorkflowAssociation wfAssociation = GetWorkflowAssociation(myList);

wfAssociation.Enabled = true

myList.UpdateWorkflowAssociation(wfAssociation);

This action will enable the workflow association.


This is perticularly very useful when you have lots of sites already created in your environment that are using the Workflow, and you re-deploy the workflow assembly. So instead of doing it through sharepoint workflow settings page, you can write a tool that leverages the Sharepoint Object Model and updates the workflow associations for all your existing sites.

2 comments:

Ai Fong said...

Sudhir, thanks so much for this blog article! It was exactly what I was looking for. :D

How's it going? :)

Josh Norris said...

Thanks a ton. I knew there was a way to do this, I just couldn't find it.

Josh