Monday, January 17, 2011

Use PowerShell to easily disable or enable SharePoint Publishing site workflows, moderation, and checkout

SharePoint Publishing Sites are strict by their very nature and have a number of security controls enabled by default such as approval workflows, moderation, and checkout. Obviously these features are important and are there for good reason, but they also have the potential to slow down the development and even the deployment process. Oftentimes you need to be able easily edit a masterpage or add a bunch of sample documents for testing. While you can disable workflows, moderation, and checkout through the user interface, it’s not something you can do very quickly, especially if you need it done recursively. The following script makes this much easier and allows you to disable or re-enable all of these features.

Steps

  1. Copy the following in Notepad.
  2. Update the last function $siteUrl and $webUrls variables.
    • $siteUrl – This is the site collection URL. For example, http://portalname
    • $webUrls – This is an array that you can use target subsites. For example, in the case of a Departments subsite with HR, IT, and Marketing subsites: "/dep", "/dep/hr","/dep/it","/dep/mkt"
  3. Toggle between $true and $false statements at the bottom of the script to disable or enable. You could also create two separate scripts.
  4. Save the file as a “.ps1” file.
  5. Run as an Administrator on your SharePoint environment.

Source Code

function RemoveApprovalWorkflow($siteUrl, $webUrl, $listName, $workFlowName)
{
$site = Get-SPSite $siteUrl
try
{
$web = $site.OpenWeb($webUrl)
try
{
$list = $web.Lists[$listName]
if(!($list -eq $null))
{
$wa = $list.WorkflowAssociations.GetAssociationByName($workFlowName, [System.Globalization.CultureInfo]::CurrentCulture)
if(!($wa -eq $null))
{
Write-Host "Removing " $wa.Name " from " $listName
$list.WorkflowAssociations.Remove($wa)
}
}
}
finally
{
$web.Dispose()
}
}
finally
{
$site.Dispose()
}
Write-Host "Finished Removing Workflow "$workFlowName" from "$siteUrl$webUrl"/"$listName
}

function AddApprovalWorkflow($siteUrl, $webUrl, $listName, $workflowTemplateName, $workflowName, $workflowTaskName, $workflowHistoryName)
{
$site = Get-SPSite $siteUrl
try
{
$web = $site.OpenWeb($webUrl)
try
{
$list = $web.Lists[$listName]
if(!($list -eq $null))
{
$wfTemp = $web.WorkflowTemplates.GetTemplateByName($workflowTemplateName, [System.Globalization.CultureInfo]::CurrentCulture);
$wf = [Microsoft.SharePoint.Workflow.SPWorkflowAssociation]::CreateListAssociation($wfTemp, $workflowName, $web.Lists[$workflowTaskName], $web.Lists[$workflowHistoryName]);
$list.WorkflowAssociations.Add($wf)
$list.DefaultContentApprovalWorkflowId = $wf.Id
$list.Update()
}
}
finally
{
$web.Dispose()
}
}
finally
{
$site.Dispose()
}
Write-Host "Finished Adding Workflow "$workflowTemplateName" to "$siteUrl$webUrl"/"$listName
}

function EnableMasterPageModerationCheckout($siteUrl, $enable)
{
$site = Get-SPSite $siteUrl
try
{
$list = $site.GetCatalog([Microsoft.SharePoint.SPListTemplateType]::MasterPageCatalog)
$list.EnableModeration = $enable
$list.ForceCheckout = $enable
$list.Update()
Write-Host "MasterPage Moderation and Checkout on "$siteUrl" set to "$enable
}
finally
{
$site.Dispose()
}
}

function EnablePublishingWorkflowsModeration($enable)
{
$siteUrl = http://portalurl #CHANGE VARIABLE
$webUrls = @("/dep", "/dep/hr","/dep/it","/dep/mkt") #CHANGE VARIABLE
$listNames = @("Pages", "Documents", "Images") #Publishing Site Default Lists
EnableMasterPageModerationCheckout $siteUrl $enable
foreach($webUrl in $webUrls)
{
EnableMasterPageModerationCheckout $siteUrl $enable
foreach($listName in $listNames)
{
if($enable)
{
AddApprovalWorkflow $siteUrl $webUrl $listName "Publishing Approval" "Page Approval" "Workflow Tasks" "Workflow History"
}
else
{
RemoveApprovalWorkflow $siteUrl $webUrl $listName "Page Approval"
}
}
}
}

EnablePublishingWorkflowsModeration $false
#EnablePublishingWorkflowsModeration $true #Comment / UnComment



 

6 comments:

  1. that background on transparent text has got to go.

    ReplyDelete
    Replies
    1. Damn, you are right. Recently changed themes and never noticed this, but was on a desktop.

      Delete
  2. Well now, if it isn't John Livingston surfacing in my googlings. Carver would say hi, but he's off riding his bike, I expect.

    ReplyDelete
  3. Ha! Good to see someone is reading it. ;) Sadly, it has been very neglected.

    ReplyDelete
  4. Hey John,

    Thanks for the helpful scripting! Saved me a lot of time and effort.

    One note to anyone else using the script. Make sure to put your site name in quotes or else the scripting will error out.

    $siteUrl = "http://portalurl" #CHANGE VARIABLE

    ReplyDelete
  5. Hi,
    is there a way to disable publishing approval workflow from a particular page in pages library??

    ReplyDelete