Sharepoint errors were found when compiling the workflow


Sharepoint: Errors were found when compiling the workflow. The workflow files were saved but cannot be run. Unexpected error on server associating the workflow.

Description

If you have a LARGE (even just few Approval processes) SharePoint Designer 2010 workflow, sometimes an error occurs stating that the workflow cannot be published.

Solution

If you have a LARGE (even just few Approval processes) SharePoint Designer 2010 workflow, sometimes an error occurs stating that the workflow cannot be published. "Errors were found when compiling the workflow. The workflow files were saved but cannot be run. Unexpected error on server associating the workflow" Wow !! "Unexpected Error" ... I bet this wont provide any further details which are helpful for troubleshooting. This error keeps popping whenever you try to publish the workflow.

REASON

This problem is caused by the large number of Types that are created during workflow compilation, for workflows with many local workflow variables. It is more common for workflows with multiple Approval Process actions, as each reconfigured Approval Process Action comes with a large set of local variables, for the different configurable property of the Approval Process.

Prior to the February 2011 CU, limits on the complexity of the workflow definition were enforced by the Microsoft .NET 3.0 workflow compiler. With the February 2011 CU, SharePoint uses the SPWebApplication UserDefinedWorkflowMaximumComplexity property to enforce a maximum number of Types in the workflow definition, and prevent compilation in these cases.

RESOLUTION

Make sure the February 2011 CU is installed - this will make available the UserDefinedWorkflowMaximumComplexity property.

By installing a CU that includes the server February 2011 CU, SharePoint administrators are now able to prevent compilation of workflows whose compilation would affect the performance of the farm. The default value of the property is 7000, but can be changed based on the needs and profile of the SharePoint farm.

The following PowerShell script can be used to adjust the value per SPWebApplication -

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$new_limit = XXXX (Set the limit here as a number);
$webapp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup("http://WebAppURL")
$webapp.UserDefinedWorkflowMaximumComplexity = $new_limit
$webapp.Update()

Try publishing the workflow now, will get through ! - but takes some time due to heavy workflow (heavy for SPD)

⚡️ Recommendation - For larger workflows, try to restructure to either have more reusable pieces, or be broken out into smaller individual small workflows.