SharePoint 2010: Synchronously Deploying Solutions (WSPs)

When developing larger SharePoint solutions (with multiple developers and environments), we normally write a series of PowerShell scripts to ensure that all our custom functionality and structure can be repeatedly deployed. The problem that commonly arises with this is that SharePoint solution files (wsps) are deployed asynchronously, meaning that later operations in PowerShell scripts can fail. A while ago, i posted a rough article on execadmsvcjobs with SharePoint 2010 (click here to view the original article). I have experimented polling the “Deployed” property of a WSP in PowerShell, however I did not find this robust in some cases.

I have recently hardened my mechanism to ensure that my PowerShell scripts wait synchronously for solutions to deploy / retract as follows (based on a excellent post by Gary LaPointe (see here))

Note that this example assumes a solution to be activated globally (i.e. not at a specific webapp), and that also deploys to the GAC.

   1: Add-PSSnapin "Microsoft.SharePoint.Powershell"

   2: $ErrorActionPreference = "stop"

   3:  

   4: $solutionPath = "C:\DummySolution\DummySolution\bin\Debug\DummySolution.wsp"

   5: $solutionName = "DummySolution.wsp"

   6: $featureName = "DummyWebPart"

   7: $urlToActivateFeature = "http://mywebsite/sites/test"

   8: $webAppUrl = "http://mywebsite"

   9:  

  10: Add-SPSolution -LiteralPath $solutionPath

  11: Install-SPSolution -Identity $solutionName -GacDeployment -WebApplication $webAppUrl

  12:  

  13: do 

  14: { 

  15:     Write-Host "Sleeping for 2 seconds to allow wsp to deploy"

  16:     Start-Sleep 2 

  17:     $solution = Get-SPSolution $solutionName

  18:     if ($solution.LastOperationResult -like "*Failed*") { throw "An error occurred deployment." }

  19:     if (!$solution.JobExists -and $solution.Deployed) { break }

  20: } 

  21: while ($true)

  22: sleep 5  

  23:  

  24: Enable-SPFeature -Identity $featureName -Url $urlToActivateFeature 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>