Tuesday, October 23, 2012

Upgrading or Redeploying SharePoint 2010 Workflows

While creating several State Machine SharePoint 2010 workflows using visual studio for a client I had some concerns related to upgrading and redeploying those workflows because as we all know, changes are inevitable!
Where following are the concerns and my solution to them
1-      How can we redeploy or upgrade workflow?
I found a nice article which goes in details of upgrading workflow approaches, and as per my analysis Versioning workflows seems to be the only feasible solution.

2-      When it’s required to version my workflows and how can I minimized it?
Versioning is required only when UI changes are done on workflow e.g. modification in state, activities etc. as this will break running instance of workflows due to failure in deserialization.

As a strategy we chose multi-layer architecture, which means whole logic in a separate Class library (business) and workflows in separate Library where workflow only defines flow as shown below.

  
This means for code level changes or bug fixes, we only redeploy business solution wsp not affecting workflows wsp.

3-      How to Version Workflow solution and any step by step instructions?
While goggling I could not found comprehensive step by step instructions.
Where following contain the step by step instruction how I implemented it.

Step 1: Upgrade Assemble 
·         Go to Workflow Solution -> AssemblyInfo Class and update assembly version and file version as shown below.


 


 Step 2: Upgrade Workflow Element.xml
Go to modified workflows and Open their Element.xml files then

·         Update 'CodeBesideAssembly' attribute assembly version.
·         Update ‘Name’ with Version number at end e.g. xyz version 1.0.0.1 (for your convenience).
·         Update 'ID' attribute with a new GUID (Tools->Create GUID).
          


Step 3: Workflow Feature Update
·         Remove any existing feature that deploy workflows from solution
·         Add a new Site Scope Feature with 'Name' e.g. PrjectNameWorkflowsVersionX.X.X.X
·         Add only modified Workflows inside this feature (Step 2 modified ones only)

Step 4: Upgrade Solution Package
   Double Click Package and then
·         Update the 'SolutionId' with a new GUID.
·         Update 'Name' with modified version.

Step 5: Deployment
·         Build and deploy the new workflow wsp solution and IIS Reset.
     Note: this will register a new version of the workflow assembly in the GAC.

Step 6: Association and No New instance
·         Associate newly deployed workflow e.g. xyz version 1.0.0.1
·         Go to Associated list Workflow Settings -> Remove workflow and Put the old version workflows as "No New instance".


Note:  As my solution contains several workflows so I created a utility for Programmatically performing step 6 operation, here are some methods that I created that might be helpful.

''' <summary>
''' To Programmatically associate workflow with a List or library
''' </summary>
Public Shared Sub AssociateWorkflow(ByVal web As SPWeb, _
                                        ByVal WorkflowGUID As String, _
                                        ByVal WorkflowName As String, _
                                        ByVal ListName As String, _
                                        ByVal TaskListName As String, _
                                        ByVal HistoryListName As String, _
                                        ByVal AllowManual As Boolean, _
                                        ByVal AutoStartChange As Boolean, _
                                        ByVal AutoStartCreate As Boolean)
        'Bind to lists.
        Dim ListToAssociate As SPList = web.Lists(ListName)
        Dim TasksListToAssociate As SPList = web.Lists(TaskListName)
        Dim workflowHistoryList As SPList = web.Lists(HistoryListName)

 'Get workflow Template
        Dim workflowTemplate As SPWorkflowTemplate = web.WorkflowTemplates(New Guid(WorkflowGUID))

        If ListToAssociate.WorkflowAssociations.Count > 0 Then
            Throw New Exception(String.Format("List '{0}' is already associated with Workflow '{1}', Please Disassociate this workflow first.", ListToAssociate.Title, ListToAssociate.WorkflowAssociations(0).Name))
        End If

        'Create workflow association.
        Dim workflowAssociation As SPWorkflowAssociation = SPWorkflowAssociation.CreateListAssociation(workflowTemplate, WorkflowName, TasksListToAssociate, workflowHistoryList)

        'Set workflow options.
        workflowAssociation.AllowManual = AllowManual
        workflowAssociation.AutoStartChange = AutoStartChange
        workflowAssociation.AutoStartCreate = AutoStartCreate

       ' Hint: WorkflowAssociation.Enabled = false means 'No new instantace' 

        'Add workflow association.
        ListToAssociate.WorkflowAssociations.Add(workflowAssociation)


    End Sub


''' <summary>
''' To programmatically start workflow already associated with a List or library
''' </summary>
Public Shared Sub ManuallyStartWorkflow(ByVal objWeb As SPWebByVal listTitle As StringByVal itemID As Integer)
        Using elevatedSite = New SPSite(objWeb.Url, SiteHelper.GetSystemUserSecruityToken(objWeb))
            Using web = elevatedSite.OpenWeb()
                web.AllowUnsafeUpdates = True
                Dim elevatedList As SPList = web.Lists(listTitle)

                'Get Associated Workflow
                 Dim item As SPListItem = elevatedList.GetItemById(itemID)
                    If item.Workflows.Count = 0 Then
                        Dim myAssociation As SPWorkflowAssociation = GetWorkflowAssociation(elevatedList.WorkflowAssociations)
                        elevatedSite.WorkflowManager.StartWorkflow(item, myAssociation, myAssociation.AssociationData)
                    End If
                End If
                web.AllowUnsafeUpdates = False
            End Using
        End Using

    End Sub
Where even after reading this article you don’t like any of the solution and you want to develop few workflows that are not complex, then go with Event Receivers you can build your logic by code resulting in better performance and you don’t have to worry about versioning or anything J