Add/Remove SharePoint Workflows thru PowerShell
You can add/remove workflows to the list or library by navigating to
List Settings -> Workflow Settings -> Add a workflow
List Settings -> Workflow Settings -> Remove a workflow
If you want to perform the same operation using PowerShell script, here is the script to add a workflow to a list
Script to Add Workflow
param( [string] $env = $(Read-Host -prompt "Specify the environment you want to run on (D-Dev, T-Test, P-Production) :") )
$env = $env.ToLower()
if($env -ne 'd' -and $env -ne 't' -and $env -ne 'p' -and $env -ne 'l') { Write-Host "ERROR: Invalid Input." } else {
$siteAddress = $null
if($env -eq 't') { $siteAddress = 'https://test.yoursite.com/' } elseif($env -eq 'd') { $siteAddress = 'https://dev.yoursite.com/' } elseif($env -eq 'p') { $siteAddress = 'https://yoursite.com/' }
echo $siteAddress
# Get the SPSite object for a given SharePoint Url $SPSite = Get-SPSite $siteAddress if($SPSite -ne $null) { # Get the root web site $Web = $SPSite.OpenWeb();
# Get the list to which we wanted to associate the workflow $SPList =$Web.Lists["Announcements"];
# Get the Approval Workflow Template by specifying current culture info # $Culture = New-Object System.Globalization.CultureInfo("en-US"); $Template = $Web.WorkflowTemplates.GetTemplateByName("Approval - SharePoint 2010",[System.Threading.Thread]::CurrentThread.CurrentCulture);
# Try to get workflow history and task list $TaskList = $Web.Lists["Workflow Tasks"]; $HistoryList = $Web.Lists["Workflow History"];
# Create workflow history list if it doesn't exist by default. if($HistoryList -eq $null) { $Web.Lists.Add("Workflow History", "My Workflow History","Lists/Workflow History", "00BFEA71-4EA5-48D4-A4AD-305DE7030140", 140, "101"); $HistoryList = $Web.Lists["Workflow History"]; }
# Create the Workflow association by using Workflow Template, Task List and History List $Association=[Microsoft.SharePoint.Workflow.SPWorkflowAssociation]::CreateListAssociation($Template, "SharePoint Approval Workflow", $TaskList, $HistoryList);
# Enable automatic workflow start when news created or changed and disable manual start option $Association.AllowManual = $false; $Association.AutoStartChange = $true; $Association.AutoStartCreate = $true;
# Provide Association Data which includes as below # 1. Approvers SharePoint group for list of approvers. # 2. Serial order Workflow. # 3. Specify the notification message while sending notifications to approvers. # 4. Automatically reject the document if it is rejected by any participant. # 5. Update the approval status after the workflow is completed (use this workflow to control content approval). $approvalAssociationData = " <dfs:myFields xmlns:xsd='https://www.w3.org/2001/XMLSchema' xmlns:dms='https://schemas.microsoft.com/office/2009/documentManagement/types' xmlns:dfs='https://schemas.microsoft.com/office/infopath/2003/dataFormSolution' xmlns:q='https://schemas.microsoft.com/office/infopath/2009/WSSList/queryFields' xmlns:d='https://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields' xmlns:ma='https://schemas.microsoft.com/office/2009/metadata/properties/metaAttributes' xmlns:pc='https://schemas.microsoft.com/office/infopath/2007/PartnerControls' xmlns:xsi='https://www.w3.org/2001/XMLSchema-instance'> <dfs:queryFields></dfs:queryFields> <dfs:dataFields> <d:SharePointListItem_RW> <d:Approvers> <d:Assignment> <d:Assignee> <pc:Person> <pc:DisplayName>Approvers</pc:DisplayName> <pc:AccountId>Approvers</pc:AccountId> <pc:AccountType>SharePointGroup</pc:AccountType> </pc:Person> </d:Assignee> <d:Stage xsi:nil='true' /> <d:AssignmentType>Serial</d:AssignmentType> </d:Assignment> </d:Approvers> <d:ExpandGroups>true</d:ExpandGroups> <d:NotificationMessage>Please have a look at submitted news.</d:NotificationMessage> <d:DueDateforAllTasks xsi:nil='true' /> <d:DurationforSerialTasks xsi:nil='true' /> <d:DurationUnits>Day</d:DurationUnits><d:CC /> <d:CancelonRejection>true</d:CancelonRejection> <d:CancelonChange>false</d:CancelonChange> <d:EnableContentApproval>true</d:EnableContentApproval> </d:SharePointListItem_RW> </dfs:dataFields> </dfs:myFields>";
$Association.AssociationData = $approvalAssociationData
# Associate the approval workflow to News List $Workflow = $SPList.WorkflowAssociations.Add($Association); if($SPSite -ne $null) { Write-Host "Workflow has been attached successfully." -foregroundcolor "Green"; $SPList.Update(); } else { Write-Host "Workflow could not be attached." -foregroundcolor "Yellow"; } } } |
Script to Remove Workflow
param( [string] $env = $(Read-Host -prompt "Specify the environment you want to run on (D-Dev, T-Test, P-Production) :") )
$env = $env.ToLower()
if($env -ne 'd' -and $env -ne 't' -and $env -ne 'p') { Write-Host "ERROR: Invalid Input." } else {
$siteAddress = $null
if($env -eq 't') { $siteAddress = 'https://test.yoursite.com/' } elseif($env -eq 'd') { $siteAddress = 'https://dev.yoursite.com/' } elseif($env -eq 'p') { $siteAddress = 'https://yoursite.com/' }
# Get the SPSite object for a given SharePoint Url $SPSite = Get-SPSite $siteAddress if($SPSite -ne $null) { # Get the root web site $Web = $SPSite.OpenWeb();
# Get the list to which we wanted to associate the workflow $SPList =$Web.Lists["Announcements"];
$workflowAssociation = $SPList.WorkflowAssociations.GetAssociationByName("SharePoint Approval Workflow", [System.Threading.Thread]::CurrentThread.CurrentCulture);
if($workflowAssociation -ne $null) { # Remove workflow association to list $SPList.RemoveWorkflowAssociation($workflowAssociation); $SPList.Update(); Write-Host "Workflow association removed Successfully." -foregroundcolor "Green" } else { Write-Host "Workflow association could not be found." -foregroundcolor "Yellow" } } } |
Comments
Anonymous
April 15, 2015
What If I dont want add the Workflow to a List? I Just want to add him into the Workflows Menu, so i can edit them in the SharePoint Designer. Is that possible?Anonymous
April 15, 2015
Without using a template. Just an empty Workflow