Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

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>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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