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.

Upgrade SharePoint Sandbox Solution

There are certain scenarios where we have to deploy the sandbox solution again with new functionality (like new column in list). Since existing solution already stores crucial data, then retracting the previous version of solution will result in losing of existing data.

Let say, you have sandbox solution which is already deployed on production with version 1.0.0.0. Now you have to made some changes in list schema of the same solution. To do that, follow these steps to make changes in solution so that upgradation works well:
  1. Make changes in your list like add new column and save it.
  2. Open feature which is linked with list.
  3. Click on “Manifest” option. Expand Edit options.
  4. Replace with the below content, change ListName with you list Name and save it. Here I am upgrading the list in which I have added one more column.
    <?xml version=”1.0″ encoding=”utf-8″ ?>
    <Feature xmlns=”http://schemas.microsoft.com/sharepoint/“>
    <UpgradeActions>
    <VersionRange BeginVersion=”0.0.0.0″ EndVersion=”1.9.9.9″>
    <ApplyElementManifests>
    <ElementManifest Location=”ListName\Elements.xml” />
    </ApplyElementManifests>
    </VersionRange>
    </UpgradeActions>
    </Feature>
  5. Now open properties of same feature (press F4 when you are at feature window). There you will find Version property. Currently it is blank and you have put there any version which lies within 0.0.0.0 to 1.9.9.9
  6. Let say I am putting 1.1.0.0 and save it.
  7. Publish the solution and create wsp package.
  8. Rename the wsp package because existing solution with same name is already present.
  9. Now open Sharepoint management shell with admin account.
  10. Execute below powershell commands to add and upgrade the new solution
    1. Add-SPUserSolution -Site <sitecollectionURL> -LiteralPath <full path of sandbox solution wsp>
    2. Update-SPUserSolution -Identity <existingSolutionName> -Site $workspaceUrl -ToSolution <newSolutionName>
  11. Go to list settings and you will find new column.

Note : Follow above steps in case you have to update content type. Changes will be done at step 4 where you have to use AddContentTypeField instead of ApplyElementManifests

For more details, please follow read below articles: