SharePoint 2013 Workflow - Refresh/Push Latest Workflow Changes (Definitions)

During my tenure on last engagement, realized that pushing the latest changes on workflows in not working as expected and the latest changes are not effective until the sub site (web) was recreated. In my case I tried options like dissociating workflows, recreating libraries, reactivating the workflow features etc, to ensure the latest changes are pushed, with no luck. After all I identified that, upon deactivation of workflow definition feature, though the feature gets deactivated the workflow definitions continue to reside and not getting replaced upon reactivation of the workflow feature. The same can be validated by navigating to the Root Folder -> wfsvc using SharePoint Manger 2013. This is intentionally made, not to disturb any existing workflows or running instances.

 

Hence I realized the real need of workaround for addressing this issue and identified one. The idea is remove the work definitions programmatically to ensure the corresponding files are wiped off from the web. Make sure the following code is integrated to remove the workflow definitions from the given web.

 /// <summary>
/// Removes the workflow definitions.
/// </summary>
/// <param name="web">The web.</param>
/// <param name="workflowDefinitionIds">The workflow definition ids.</param>
public static void RemoveWorkflowDefinitions(SPWeb web, List<Guid> workflowDefinitionIds)
{
 var workflowServicesManager = new WorkflowServicesManager(web);
 
 if (!workflowServicesManager.IsConnected)
 {
 var notConnectedEx = new NotConnectedException();
 Logger.LogCriticalException(notConnectedEx, ErrorCategory.CommonOperation);
 throw notConnectedEx;
 }
 
 var workflowDeploymentService = workflowServicesManager.GetWorkflowDeploymentService();
 
 foreach (var workflowDefinitionId in workflowDefinitionIds)
 {
 var workflowDefinition = workflowDeploymentService.GetDefinition(workflowDefinitionId);
 
 if (workflowDefinition != null)
 {
 workflowDeploymentService.DeleteDefinition(workflowDefinitionId);
 }
 }
}

And now we can call the method to remove the definitions with specific IDs as follows.

 RemoveWorkflowDefinitions(web, new List<Guid> { new Guid("D7CA92FA-1220-4959-A806-719CBDE3DB6D") });

Note: Before deletion of any WF definition by calling the above line, Please make sure that all the workflow instances of the given workflow definition is removed/disassociated from the list/libraries .

Upon successful call to this method, you can validate and find that the workflow defintions are removed from the web Root Folder -> wfsvc.

 

Upon this, you  are good to go and activate the workflow definition feature that restores the updated (changed) version of the workflow on given site.