Subscribe:      
 

Run a Site Workflow with PowerShell and the Windows Task Scheduler

Written By: Chris Beckett -- 7/5/2011 -- join -- contribute -- (700) comments -- printer friendly version

Rating: Rate --

Categories: Configurations, PowerShell, Programming and Customizations, SharePoint 2010, SharePoint Foundation 2010, System Administration, Workflow

< Prev - 1 | 2 | - Next > | Become a paid author

Problem

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. 

Solution

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 Site workflow using PowerShell that can be easily scheduled using the Windows Task Scheduler.

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.

# SharePoint.Workflow.Start-SPSiteWorkflow
param (
    [string]$Url = "http://sharepoint/demos/workflow",
    [string]$Workflow = "Sample Site Workflow"
)

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

# get the workflow assocation from the target site/web
$site = Get-SPSite $Url
$culture = [System.Globalization.CultureInfo]::InvariantCulture
$wfAssociation = $site.RootWeb.WorkflowAssociations.GetAssociationByName($Workflow, $culture)

if ( $wfAssociation )
{
    # initialize and optionally update association data
    $assocData = $wfAssociation.AssociationData

    # start the workflow
    $wfRunOption = [Microsoft.SharePoint.Workflow.SPWorkflowRunOptions]::Asynchronous
    $site.WorkflowManager.StartWorkflow($site, $wfAssociation, $assocData, $wfRunOption)
}
else
{
    Write-Error "Workflow association not found on target web site."
}

Running a site workflow from code can be broken into 3 steps:

  1. Get the Workflow Association on the target site from the SPWeb.WorkflowAssociations collection.
  2. Build the Workflow Association Data string.
  3. 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 - 1 | 2 | - Next >



Learn more about SharePoint



Sponsor Information




Copyright (c) 2010-2017 Edgewood Solutions, LLC All rights reserved
privacy | disclaimer | copyright | advertise | contribute | feedback | about
Some names and products listed are the registered trademarks of their respective owners.


MSSharePointTips.com | MSSQLTips.com