Jun 11, 2013

Publish SharePoint 2010 workflows from Powershell

I came across this problem when we moved a site collection from one domain to another and all the workflows needed to be republished.

The Powershell script is pretty straightforward, except for one gotcha:  when setting the URI for a webservice proxy in Powershell, the webservice URL is always reset to the root webservice.

 Eg. http://server/subsite/_vti_bin/webpartpages.asmx  becomes http://server/_vti_bin/webpartpages.asmx.  You can later change the proxy object url to the correct URL.

 This showed up in the SharePoint logs as "SOAP exception: System.IO.FileNotFoundException: The system cannot find the specified file." and the webservice would only return a generic SOAPException. 

Here's the Powershell script:

$site = Get-SPSite http://server
$username = "domain\admin"
$password = "apassword"
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
$URI = "http://server/_vti_bin/webpartpages.asmx"
$proxy = New-WebServiceProxy -Uri $URI -Credential $cred


foreach($web in $site.AllWebs) {
    Try {
        $subFolders = $web.Folders["Workflows"].SubFolders;
       
        foreach($folder in $subFolders) {
            if($folder.Name -ne "Forms") {
                $xomlConfigFile = $folder.Files[$folder.Name+".xoml.wfconfig.xml"]
                Write-Host $xomlConfigFile.Name $xomlConfigFile.UIVersionLabel
                # The web service proxy sets the URL to root, so we reset it to the subsite
                $proxy.Url = $web.Url+"/_vti_bin/webpartpages.asmx"
                $result = $proxy.AssociateWorkflowMarkup($xomlConfigFile.Url,"V"+$xomlConfigFile.UIVersionLabel)
                Write-Host $result
            }
        }
    } Catch {
   
    }

        $web.Dispose()

}

$site.Dispose()

1 comment:

  1. greetings!

    the script doesn't do anything...the requests are successful but looking at the last modified date in SPD nothing is changed. Also, I believe that "UIVersionLabel" has to be increased by 1, but when doing so I get "Failed to open config file" error. Any thoughts?

    Much appreciated,
    Adrian

    ReplyDelete