Pages

Search This Blog

Tuesday, October 23, 2012

Attach Disposition Workflow on a content type using Powershell

Hi Friends,

Many times we are needed to attach a "Disposition Workflow" with a Content Type in SharePoint 2010. This is pretty easy to accoplish. Please find the PowerShell to do this.

# Method Calling
AddWorkflowToContentType $site $ContentTypeName "Disposition Approval" "WFA" $TaskListName

#Method Definition
function AddWorkflowToContentType($site, $ctName, $WfName, $WfAssociationName, $TaxTaskList)
   {
    [void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SharePoint')
    [void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.Office.Policy')

    [Guid]$wfTemplateId = New-Object Guid
    $web = $site.RootWeb
       $ct = $web.ContentTypes[$ctName]
       $culture = New-Object System.Globalization.CultureInfo("en-US")
       $template = $site.RootWeb.WorkflowTemplates.GetTemplateByName($WfName, $culture)
   
       if($template -ne $null)
       {                       
      $tasklist = $TaxTaskList
                     $historylist = "Workflow History"
                    
                        # Checking For WorkFlow History List; If not exists then create it
                        if(!$web.Lists[$historylist])
                        {
                            $web.Lists.Add($historylist, "A system library used to store workflow history information that is created in this site.  It is created by the Publishing feature.",
                            "WorkflowHistory", "00BFEA71-4EA5-48D4-A4AD-305CF7030140", 140, "100")
                           
       if (!$web.Features["00BFEA71-4EA5-48D4-A4AD-305CF7030140"]) {
                                Enable-SPFeature -Identity WorkflowHistoryList -Url $web.Url
                            }
    
                            $wfHistory = $web.Lists[$historylist]
                            $wfHistory.Hidden = $true
                            $wfHistory.Update()
                        }

                        # Checking For WorkFlow Task List; If not exists then create it
                        if(!$web.Lists[$tasklist])
                        {
                            $web.Lists.Add($tasklist, "This system library was created by the Publishing feature to store workflow tasks that are created in this site.", "WorkflowTasks", "00BFEA71-A83E-497E-9BA0-7A5C597D0107", 107, "100")
                        }
   
                        # Creating Workflow Association with Content Type
                        $association = [Microsoft.SharePoint.Workflow.SPWorkflowAssociation]::CreateSiteContentTypeAssociation($template, $WfAssociationName, $web.Lists[$tasklist], $web.Lists[$historylist])
                       
      $association.AllowManual = $false
                        #$association.AutoStartCreate = $true
                        $ct.AddWorkflowAssociation($association)
                        $ct.UpdateWorkflowAssociationsOnChildren($true, $true, $true, $false)
                        $association.Enabled=$true
                        $ct.Update()
                        $web.Update()
                        #$ct.WorkflowAssociations[0]           
       }
       else
       {
        Write-Error "Workflow Template not found"
                    Add-Content $logFileName "Workflow Template not found."
       }
   }

Happy Coding!!!

Prabhat

No comments:

Post a Comment