locked
How to Import SharePoint Designer Workflow (vwi) to new Website programmatically ? RRS feed

  • Вопрос

  • I have exported some SharePoint designer workflows (vwi) and I made some changes to its XOML (Like changing old Lists GUID to the new Lists Guid , changing old URLS to new ones .... etc ) and What I am trying to do is to import those exported vwi files to new SharePoint 2016 Site Collection then to associate each with a specific List.

    Manually it is done by opening SharePoint Designer > Import Workflow from Visio File > Choose associated List > Publish 

    but I want to do it programmatically or using powershell .

    any one went through this before?

    28 февраля 2019 г. 6:58

Все ответы

  • Hi,

    We can export the workflow as .xaml file and modify the file, then deploy it using PowerShell.

    if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) 
    {
        Add-PSSnapin "Microsoft.SharePoint.PowerShell"
        Write-Host "Added Snapin for SharePoint" -ForegroundColor Yellow
    }
    
    function DeployListWorkflow($workflow_xaml, $url, $list_name, $wf_displayname, $overwrite){
        $w = Get-SPWeb $url
        Write-Host "Get web for" $url -ForegroundColor Green
    
        $wfm = New-Object Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager($w)
        Write-Host "Workflow Service Manager was found:" ($wfm -ne $null) -ForegroundColor @("Red", "Green")[$wfm -ne $null]
    
        $wfd = $wfm.GetWorkflowDeploymentService()
        Write-Host "Workflow Deployment service discovered:" ($wfd -ne $null) -ForegroundColor @("Red", "Green")[$wfd -ne $null]
    
        #Check if workflow exists
        $publishedWorkflows = $wfd.EnumerateDefinitions($false)
    
        $wfdef = $publishedWorkflows | Where-Object {$_.DisplayName -eq $wf_displayname} | Select-Object -First 1
        $wf_exists = $wfdef -ne $null
    
        if ($wf_exists -and -not $overwrite){
            Write-Host "This workflow already exists. Please set other workflow name or set `$overwrite = `$true." -ForegroundColor Red
        }
        else
        {
            # Publishing workflow
            if ($wfdef -eq $null){
                $wfdef = New-Object Microsoft.SharePoint.WorkflowServices.WorkflowDefinition
            } else {
                Write-Host "Workflow definition will be overwritten." -ForegroundColor Yellow
            }
    
            $wf_targetlist = $w.Lists[$list_name]
    
            $wfdef.DisplayName = $wf_displayname
            $wfdef.Xaml = Get-Content $workflow_xaml -Encoding UTF8
            $wfdef.Properties["Overriide"] = $true
            # additional properties for view binding list in SPD #
            if ($wf_targetlist -ne $null){
                $wfdef.SetProperty("RestrictToType", "List")
                $wfdef.SetProperty("RestrictToScope", $wf_targetlist.ID.ToString().ToUpper())
            }
            #***********************#
    
            $wf_id = $wfd.SaveDefinition($wfdef)
            Write-Host "Workflow definition id:" $wf_id
            $wfd.PublishDefinition($wf_id)
    
            # Binding to list
            if ($wf_targetlist -eq $null){
                Write-Host ("List <{0}> not found. Binding failed." -f $list_name) -ForegroundColor Red
            }
            else {
                $wfs = $wfm.GetWorkflowSubscriptionService()
                Write-Host "Workflow Subscription service discovered:" ($wfs -ne $null) -ForegroundColor @("Red", "Green")[$wfs -ne $null]
                $wf_subs = $wfs.EnumerateSubscriptions() | Where-Object {$_.DefinitionId -eq $wf_id -and $_.EventSourceId -eq $wf_targetlist.ID}
                # Delete previous subscriptions
                $wf_subs | ForEach-Object {$wfs.DeleteSubscription($_.Id)}
                $wf_tasklist = $w.Lists | Where-Object {$_.Title -eq "Задачи рабочего процесса"} | Select-Object -First 1
                Write-Host "Workflow task list found:" ($wf_tasklist -ne $null) -ForegroundColor @("Red", "Green")[$wf_tasklist -ne $null]
    
                $wf_histlist = $w.Lists | Where-Object {$_.Title -eq "Журнал рабочего процесса"} | Select-Object -First 1
                Write-Host "Workflow history list found:" ($wf_histlist -ne $null) -ForegroundColor @("Red", "Green")[$wf_histlist -ne $null]
    
                $wfsub = New-Object Microsoft.SharePoint.WorkflowServices.WorkflowSubscription
                $wfsub.DefinitionId = $wf_id
                $wfsub.Name = $wf_displayname
                $wfsub.Enabled = $true
                $eventTypes = New-Object System.Collections.Generic.List[String]
                $eventTypes.Add("WorkflowStart")
                $wfsub.EventTypes = $eventTypes
                $wfsub.EventSourceId = $wf_targetlist.ID.ToString()
                $wfsub.SetProperty("TaskListId", $wf_tasklist.ID.ToString())
                $wfsub.SetProperty("HistoryListId", $wf_histlist.ID.ToString())
                $wfsub.SetProperty("ListId", $wf_targetlist.ID.ToString())
                $wfsub.SetProperty("Microsoft.SharePoint.ActivationProperties.ListId", $wf_targetlist.ID.ToString())
                # additional properties #
                $wfsub.SetProperty("SharePointWorkflowContext.ActivationProperties.SiteId", $w.Site.ID.ToString())
                $wfsub.SetProperty("SharePointWorkflowContext.ActivationProperties.WebId", $w.ID.ToString())
                #***********************#
                $wfsub_id = $wfs.PublishSubscriptionForList($wfsub, $wf_targetlist.ID)
                Write-Host "Workflow subscription id:" $wfsub_id
            }
    
            Write-Host "Finished." -ForegroundColor Green
        }
    }
    
    function ExtractWorkflowXaml($url, $wf_displayname, $workflow_xaml_path){
        $w = Get-SPWeb $url -ErrorAction SilentlyContinue
        if ($w -eq $null){
            Write-Host "Get web for" $url "failed" -ForegroundColor Red
            return
        }
        Write-Host "Get web for" $url -ForegroundColor Green
    
        $wfm = New-Object Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager($w) -ErrorAction SilentlyContinue
        Write-Host "Workflow Service Manager was found:" ($wfm -ne $null) -ForegroundColor @("Red", "Green")[$wfm -ne $null]
        if ($wfm -eq $null) { return }
    
        $wfd = $wfm.GetWorkflowDeploymentService()
        Write-Host "Workflow Deployment service discovered:" ($wfd -ne $null) -ForegroundColor @("Red", "Green")[$wfd -ne $null]
        if ($wfd -eq $null) { return }
    
        $wf_def = $wfd.EnumerateDefinitions($false) | Where-Object { $_.DisplayName -eq $wf_displayname } | Select-Object -First 1
        Write-Host "Workflow definition was found:" ($wf_def -ne $null) -ForegroundColor @("Red", "Green")[$wf_def -ne $null]
        if ($wf_def -ne $null){
            try {
            $wf_def.Xaml | Out-File $workflow_xaml_path -ErrorVariable ProcessError
            }
            catch{ }
            Write-Host "Export workflow.xaml has been" @("failed", "successfully completed")[$ProcessError.Count -eq 0] -ForegroundColor @("Red", "Green")[$ProcessError.Count -eq 0]
        }
    }
    
    
    #ExtractWorkflowXaml "http://mysp.com/source" "Test workflow" "C:\spmetadata\workflow.xaml"
    #DeployListWorkflow "C:\spmetadata\workflow.xaml" "http://mysp.com/sandbox" "Test Workflow List" "My New Workflow" $true

    More information is here:

    https://github.com/DarkGenius/sp-powershell/blob/master/workflows/deploy_workflow.ps1

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    1 марта 2019 г. 2:39