Dan Sanders
  • Home
  • About
  • Contact
  • – PowerShell
  • – SharePoint
  • – Workflow
Copyright © 2016 Dan Sanders. All Rights Reserved.
Dan Sanders
  • Home
  • About
  • Contact
  • – PowerShell
  • – SharePoint
  • – Workflow

Associate your 2013 workflow to every library/list through PowerShell

byDan Sanders inPowershell, SharePoint, Workflow posted29 February, 2016
0
0
Associate your 2013 workflow to every library/list through PowerShell

The workflow I have deployed/enabled globally is a SharePoint 2013 workflow.
SharePoint 2013 workflows are hosted on the Workflow Manager platform.

To add the workflow on all Libraries, we need to add a new workflow subscription to the workflow manager using the ID of our Workflow definition.

To find the workflow definition ID:

$Web = Get-SPWeb http://site/web/ #Web where the Workflow feature is enabled.
$wfm = New-object Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager($web)
$defSevice = $wfm.GetWorkflowDeploymentService()
$wfDefs = $defSevice.EnumerateDefinitions($false) 
$wfDefs | Format-Table ID, DisplayName

Set the workflow definition ID from the above result into the $WorkflowDefinition variable below.

$Web = Get-SPWeb http://intranet/web/ 
$Web.webs | ForEach-Object {
  $_.webs | ForEach-Object { #I'm targeting 3rd Web down.
    $wfm = New-object Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager($_)
    $defSevice = $wfm.GetWorkflowDeploymentService()
    $wfDefs = $defSevice.EnumerateDefinitions($false) 
    ### Update below variable with yours
    $WorkflowDefinition = "Workflow Definition ID goes here"
    ###
    $wfDef = $wfDefs | where {$_.Id -eq $WorkflowDefinition}

    $wfTaskList = $_.Lists["Workflow Tasks"]; #Task and History lists must exist, see note below.
    $wfHistoryList = $_.Lists["Workflow History"]

    $_.lists | where { $_.BaseTemplate -eq "DocumentLibrary" } | ForEach-Object {
      if($_.title -eq "Site Assets")
      {
        write-host "Skiping Site Assets"
      }
      else
      {
        $wfSubService = $wfm.GetWorkflowSubscriptionService()
        
        #Create Workflow Subscription 
        $sub = New-object Microsoft.SharePoint.WorkflowServices.WorkflowSubscription
        $sub.DefinitionId = $WorkflowDefinition
        $sub.Enabled = $true
        $sub.Name = $wfDef.DisplayName

        #Build start options
        $startOptions = New-Object "System.Collections.ObjectModel.Collection[System.String]"
        # $startOptions.Add("ItemAdded") #When item added
        # $startOptions.Add("ItemUpdated") #When item updated
        $startOptions.Add("WorkflowStart") #Allow manual start
        $sub.EventTypes = $startOptions

        $sub.SetProperty("HistoryListId", $wfHistoryList.Id)
        $sub.SetProperty("TaskListId", $wfTaskList.Id)
       
        try{
          $wfSubService.PublishSubscriptionForList($sub, $_.Id);
          write-host "Workflow" $wfDef.DisplayName "successfully associated to" $_.title
        }
        catch{
          write-host "Workflow FAILED to associate for" $_.title
        }
      } #End Else 
    } #End Lists Loop
  } #End $_.webs Loop
} #End $Web.webs Loop

The task/history lists must exist in each library before running the above code.
Enabling the Publishing feature on a site creates these for you. You can do this through PowerShell here. (refer to bottom of post)

Resources:
– StackExchange question answered by Caroline which is where I got the basis of the above code from.
– Great post on Get-SPScripts which shows adding a workflow through PowerShell and seems to add the Tasks/History list if they don’t exist.

Document LibrariesListsPowerShellSharePoint 2013SP2013 WorkflowWorkflow
Share this :

Related Posts

0
24 June, 2013
Customizing the Quick Launch

The workflow I have deployed/enabled globally is a SharePoint 2013 workflow. SharePoint 2013...

368 Comments
0
Fix SharePoint 2013 UPS Stuck on Starting
4 July, 2018
Fix SharePoint 2013 UPS Stuck on Starting

The workflow I have deployed/enabled globally is a SharePoint 2013 workflow. SharePoint 2013...

54 Comments
0
Adding a column to a Content Type (CT) at a library level with PowerShell.
10 February, 2016
Adding a column to a Content Type (CT) at a library level with PowerShell.

The workflow I have deployed/enabled globally is a SharePoint 2013 workflow. SharePoint 2013...

67 Comments
0
28 April, 2016
Disable Loopback check – UPA Picture Sync Error

The workflow I have deployed/enabled globally is a SharePoint 2013 workflow. SharePoint 2013...

73 Comments
0
PowerShell for every List in each Web of a SiteCollection
28 February, 2016
PowerShell for every List in each Web of a SiteCollection

The workflow I have deployed/enabled globally is a SharePoint 2013 workflow. SharePoint 2013...

37 Comments
0
4 February, 2016
Adding a ContentType to each Library with PowerShell

The workflow I have deployed/enabled globally is a SharePoint 2013 workflow. SharePoint 2013...

30 Comments
0
Quick Edit button grayed out
3 February, 2016
Quick Edit button grayed out

The workflow I have deployed/enabled globally is a SharePoint 2013 workflow. SharePoint 2013...

No comment
0
17 September, 2013
Remove Drop Off Library and fix up the QuickLaunch in SharePoint 2010

The workflow I have deployed/enabled globally is a SharePoint 2013 workflow. SharePoint 2013...

67 Comments
0
Creating a view for each Document Library in a Site Collection
3 February, 2016
Creating a view for each Document Library in a Site Collection

The workflow I have deployed/enabled globally is a SharePoint 2013 workflow. SharePoint 2013...

135 Comments
0
24 June, 2013
Enable Fly-Out for Quick Launch

The workflow I have deployed/enabled globally is a SharePoint 2013 workflow. SharePoint 2013...

96 Comments

Leave a Comment! Cancel reply

You must be logged in to post a comment.

Recent Posts

  • SharePoint Add-in 401 Unauthorized
  • Content Database and Site Collection Report
  • SharePoint helpful hidden URLs

Tags

Config (1) Content Types (2) Design (3) Document Libraries (7) Document Sets (1) How-To (1) Lists (4) Office365 (1) PnP (1) PowerShell (12) Quick Edit (2) REST (1) SharePoint (11) SharePoint 2010 (11) SharePoint 2013 (14) SharePoint Foundation (2) SharePoint Online (1) SP2010 Workflow (2) SP2013 Workflow (3) Systems (2) User Profile Service (1) Views (5) Windows Server (1) Workflow (2)

Archives

  • November 2020
  • December 2018
  • September 2018
  • July 2018
  • September 2017
  • August 2016
  • April 2016
  • March 2016
  • February 2016
  • January 2016
  • September 2013
  • July 2013
  • June 2013