1

I have created a simple reusable workflow using designer and deployed its solution on the server. I have done it using Powershell. Now I wanted to know; how can I attach this workflow to a list or a Library?

1

below script works fine for a list and all content type:

function AddWorkflowToLibraries ($SiteCollection, $ctName, $WfName, $WfAssociationName)
{
$site = Get-SPSite $SiteCollection
[Guid]$wfTemplateId = New-Object Guid

#Step through each web in site collection
$site | Get-SPWeb -limit all | ForEach-Object {
    $web = $_
    $_.Lists | ForEach-Object{
        if($_.AllowContentTypes -eq $true)
        {
            if($_.ContentTypes.Item("$ctName") -ne $null)
            {
                write-host "Enabling workflow on" $_.Title "in" $_.ParentWebUrl

                $ct = $_.ContentTypes[$ctName]
                $culture = New-Object System.Globalization.CultureInfo("en-US")
                $template = $site.RootWeb.WorkflowTemplates.GetTemplateByName($WfName, $culture)

                if($template -ne $null)
                {
                    $tasklist = "Tasks"
                    $historylist = "Workflow History"

                    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()
                    }

                    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")
                    }
                    $association = [Microsoft.SharePoint.Workflow.SPWorkflowAssociation]::CreateListAssociation($template, $wfName, $web.Lists[$tasklist], $web.Lists[$historylist])
                    $association.AllowManual = $true

                    $_.AddWorkflowAssociation($association)
                    $_.Update()
                }
                else
                {
                    Write-Error "Workflow Template not found"
                }
            }
        }
    }
}
}

AddWorkflowToLibraries <Site Name> <Content Type Name> <Workflow Template Name> <Association Name>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.