SharePoint : Powershell snippet to upgrade sandbox solution

In previous article Upgrade SharePoint Sandbox Solution, we have explained how to upgrade the sandbox solution so that necessary changes must reflected properly. To upgrade the solution, do not deploy it from Visual Studio because it will retract the previous activated solution from solutions gallery and add new one.

You can use below powershell snippet, which will upgrade the existing sandbox solution with new one. Since solution package (*.wsp) have same solution id even if we rename them. So here we are finding the already activated sandbox solution in solution gallery from new solution package.

function Update-SPUserSolutons
		param($solutionFolderPath, $siteUrl)
		$files = Get-ChildItem -Path $solutionFolderPath -Recurse -File
		foreach ($file in $files)
			if($(Get-SPUserSolution -Site $siteUrl -Identity $file.Name -ErrorAction SilentlyContinue) -eq $null)
				Add-SPUserSolution -Site $siteUrl -LiteralPath $file.Fullname
                                # Get already activated solution from solution gallery
				$solutionName = Get-SPUserSolutionId -siteUrl $siteUrl -name $file.Name
				write-host "Upgrading solution $solutionName to $file.Name"
				Update-SPUserSolution -Identity $solutionName -Site $siteUrl -ToSolution $file.Name
				write-host "$file.Name upgraded"				

function Get-SPUserSolutionId
		param($siteUrl, $name)
		$newSolution = Get-SPUserSolution -Site $siteUrl -Identity $name;
		$solution = Get-SPUserSolution -Site $siteUrl |?{$_.SolutionId -eq $newSolution.SolutionId -and $_.Status -eq "Activated"}|select -first 1
		return $solution.Name;

#Call below function to upgrade solution
Update-SPUserSolutons -solutionFolderPath "E:\SandboxSolution\" -siteUrl "http:\\your-sitecollection-url" 

Note : In solutionFolderPath parameter, provide the folder path where you have placed your wsp. Make sure you have only wsp packages in that folder.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s