Ensure_List_ContentType_WorkflowAssociation

Language:
PowerShell
Keywords:
SharePoint
Code Snippet

## Ensures that a Workflow is Associated to a Content Type.. if it does, do nothing; if it does not, add it
Function Ensure_List_ContentType_WorkflowAssociation( [Microsoft.SharePoint.SPContentType]$tContentType
                                                    , [Microsoft.SharePoint.Workflow.SPWorkflowTemplate]$tWorkflowTemplate
                                                    , $wfAssociationName
                                                    , [Microsoft.SharePoint.SPList]$wfTaskList
                                                    , [Microsoft.SharePoint.SPList]$wfHistoryList
                                                    , [System.Boolean]$wfStartManual = $true
                                                    , [System.Boolean]$wfStartManualRequireManageListPermissions = $false
                                                    , [System.Boolean]$wfStartOnCreate = $false
                                                    , [System.Boolean]$wfStartOnChange = $false
                                                    ) {
   #if ($tContentType.WorkflowAssociations.GetAssociationByBaseID($tWorkflowTemplate.BaseId) -eq $null) {
    if ($tContentType.WorkflowAssociations.GetAssociationByName("Workflow Association Name Here", [System.Globalization.CultureInfo]::CurrentCulture) -eq $null) {
        $tWFAssocation = [Microsoft.SharePoint.Workflow.SPWorkflowAssociation]::CreateListContentTypeAssociation($tWorkflowTemplate, $wfAssociationName, $wfTaskList, $wfHistoryList)
        
        # Set the Manual Start options
        $tWFAssocation.AllowManual = $wfStartManual
        if ($wfStartManualRequireManageListPermissions) {
            $tWFAssocation.PermissionsManual = [Microsoft.SharePoint.SPBasePermissions]::ViewListItems -bor
                                               [Microsoft.SharePoint.SPBasePermissions]::EditListItems -bor
                                               [Microsoft.SharePoint.SPBasePermissions]::ManageLists
        } else {
            $tWFAssocation.PermissionsManual = [Microsoft.SharePoint.SPBasePermissions]::EditListItems
        }
        # Set the Auto Start options
        $tWFAssocation.AutoStartCreate = $wfStartOnCreate
        $tWFAssocation.AutoStartChange = $wfStartOnChange
        
        Write-Output([System.String]::Format("List Content Type [{0}] : Adding Workflow Association [{1}] to Workflow [{2}]", $tContentType.Name, $wfAssociationName, $tWorkflowTemplate.Name))
        $tContentType.AddWorkflowAssociation($tWFAssocation) | Out-Null
        $tWFAssocation.Enabled = $true
        # $tContentType.Update()
    }
} # Function Ensure_List_ContentType_WorkflowAssociation(...)


Created 2011-10-13
comments powered by Disqus
Login