Run a Site Workflow with PowerShell and the Windows Task Scheduler
Written By: Chris Beckett -- 7/5/2011 --
(2791) comments --
Categories: Configurations, PowerShell, Programming and Customizations, SharePoint 2010, SharePoint Foundation 2010, System Administration, Workflow
< Prev -
- Next >
| Become a paid author
SharePoint 2010 introduced Site Workflows; workflows that can
be manually started by a Site Administrator and are not bound to a
specific list or library. Site Workflows can be useful for automating
periodic administrative jobs, or extending business workflows with
periodic batch processes. I like to think of them of being sort of like
a timer job, but for a specific Site. The problem with Site workflows
is that there is no out-of-box ability to schedule them.
The SharePoint Server Object API provides the ability to
interact with Workflows via Server-side code. This makes it easy to
interact with the Site Workflow Manager to start a workflow using
PowerShell. In this tip, I am going to demonstrate how we can start a
using PowerShell that can be easily scheduled using the Windows Task
Create a PowerShell Script
The following script demonstrates how to start a workflow
using the SharePoint Object API from PowerShell. For
my tip, I have already created a simple Site workflow named "Sample
Site Workflow". This script can be used to execute any Site workflow
with default association data.
[string]$Workflow = "Sample Site
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction
# get the workflow assocation from the target site/web
$site = Get-SPSite $Url
$culture = [System.Globalization.CultureInfo]::InvariantCulture
if ( $wfAssociation )
# initialize and optionally update association data
$assocData = $wfAssociation.AssociationData
# start the workflow
$site.WorkflowManager.StartWorkflow($site, $wfAssociation, $assocData,
Write-Error "Workflow association not found on target web site."
Running a site workflow from code can be broken into 3 steps:
- Get the Workflow Association on the target site from
the SPWeb.WorkflowAssociations collection.
- Build the Workflow Association Data string.
- Start the workflow using the SPSite.WorkflowManager object.
The only tricky part of this code is the SPWorkflowAssocation.AssociationData
property. This string value most commonly contains Xml
representing serialized data from the
Association form configured for the workflow. The appropriate elements
and attributes consumed by the workflow are specific to the workflow,
and how the form is serialized.
For SharePoint Designer 2010 workflows, this property
will contain the Xml and default association parameter values based on
the schema for the auto-generated InfoPath form. If you want
to use the default
association parameters, you can simply
read the default value from the workflow association. This is what I
have done in my sample.
< Prev -
- Next >