Question

Summary: How do I create SharePoint 2013 Workflow Associations through Powershell? SharePoint 2013 workflows do not show up on the WorkflowTemplates list of the SPWeb.

I am attempting to associate a SharePoint 2013 Workflow to a document library using PowerShell.

The workflow was deployed to a on-premise SharePoint farm using a No Code Sandbox Solution.

If I go through the usual Web Interface the Workflow is visible and, if I follow through the association, functional.

However, if I try to get the list SPWeb.WorkflowTemplates I am shown only the SP2010 style workflow templates.

I'm automating the deployment and want to create the association through PowerShell so that I don't need to write a step by step for a non-sharepoint savvy deployer (to be followed in various environments [they actually have 3 tiers - I love bank clients]).

As it stands, creating the association declaratively as part of the feature deployment works. I am wondering how this could be done if that were not an alternative (also, declarative association creates 3 associations - one for each type of event / don't like that).

Was it helpful?

Solution

This is because SharePoint 2013 Workflows are hosted on Workflow Manager. So you need to add a new workflow subscription to Workflow Manager. I found the following articular useful to workout how to do this. Although this uses the CSOM rather than PowerShell.

Working with the SharePoint 2013 Workflow Services Client Side Object Model

Here is a PowerShell examples. To get the workflows installed use the WorkflowDeploymentService:

    $web = get-spweb "http://yourWebUrl"    
    $wfm = New-object Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager($web)
    $defSevice = $wfm.GetWorkflowDeploymentService()
    $wfDefs = $defSevice.EnumerateDefinitions($false)
    $wfDef = $wfDefs | where {$_.Id -eq "ID of the WorkflowDefinition"}

Once you have found the id of the WorkflowDefinition you would like to associate to the list you can create a new WorkflowSubscription and publish it to your list using the WorkflowSubscription service:

    $wfsubService = $wfm.GetWorkflowSubscriptionService()
    $wfTaskList = $web.Lists["Workflow Tasks"];
    $wfHistoryList = $web.Lists["Workflow History"];
    $list = $web.List["List you want to add to"];

    #Create Workflow Subscription 
    $sub = New-object Microsoft.SharePoint.WorkflowServices.WorkflowSubscription
    $sub.DefinitionId = {this is the id of the 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)

    $wfSubService.PublishSubscriptionForList($sub, $list.Id);
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top