Thursday, July 2, 2015

Starting a SharePoint Designer site WorkFlow from PowerShell


I had to schedule a SharePoint Designer site workflow with a “no code” solution. Normally, when I need to set a scheduled workflow I will create a SharePoint timer job and schedule it to run. The requirement for this particular client meant I couldn't add any SharePoint code. This ruled out using a SharePoint timer job. A search on the Internet yielded no results and no one already had a PowerShell script created so I wrote one.

 Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
function start-siteworkflow{
Param(
[Parameter(Mandatory = $true, HelpMessage="Enter the Site URL that contains the site workflow")][string]$RootSite,
[Parameter(Mandatory = $true,HelpMessage="Enter the name of the site workflow")][string]$workFlowName,
[Parameter(Mandatory = $false, HelpMessage="Leave Blank")][AllowEmptyString()][string]$location


)
$site = get-spsite $RootSite
$web = $site.OpenWeb()
$assoc = $web.WorkflowAssociations.GetAssociationByName($workFlowName, [CultureInfo]::'InvariantCulture')
$params = [xml]""

$site.WorkFlowManager.StartWorkflow($location, $assoc, $params, 'Asynchronous')

}


start-siteworkflow 

The script requires two parameters, the root site where the workflow is saved and the name of the workflow. There is a third parameter that is not mandatory and is only there to send a Null to the StartWorkflow method. When the script is run, if that is missing the workflow will fail. start-siteworkflow -RootSite http://site -workFlowName “Workflow Name” The workflow will run with the SharePoint Timer service which is usually within 5 minutes. Enjoy!