How to Deploy the SandBox Solutions in SharePoint 2013 Using PowerShell


Sathish Nadarajan
SharePoint MVP
Published On :   06 Dec 2015
Visit Count
Today :  3    Total :   6226
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

Sharegate: Kick-Ass Tool
Think Your SharePoint & Office 365 Are Secure ? Find Out Now!


In the earlier article, we saw how to deploy the Farm Solution. In the same manner, there are certain changes required for the SandBox Solution. Let us have a look at them as well.

 ##================================================================================================
 ## Description	: To do the below two items.
     #1. Deploy the SandBox Solutions
 ## Author		: Sathish Nadarajan
 ## Date			: 20-Nov-2015
 ##================================================================================================
 
 ##============================================ Setup Input Paths ===========================================================
 
 cls
  
 $Host.UI.RawUI.WindowTitle = "-- Deploy the SandBox Solutions into Site Collection --"
 
 $StartDate = Get-Date
 Write-Host -ForegroundColor White "------------------------------------"
 Write-Host -ForegroundColor White "| Deploy the SandBox Solutions into Site Collection |"
 Write-Host -ForegroundColor White "| Started on: $StartDate |"
 Write-Host -ForegroundColor White "------------------------------------"
 
 $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
 $LogFile = ".\DeploySandBoxSolution-$LogTime.rtf"
 
 #start-transcript $logfile
 
 $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
 Set-Location $scriptBase
 
 $ErrorActionPreference = "Stop"
 
 function AddPowerShellSnapin()
 {
     try
     {
         Write-Host "Adding PowerShell Snap-in" -ForegroundColor Green
         # Try to get the PowerShell Snappin.  If not, then adding the PowerShell snappin on the Catch Block
         Get-PSSnapin "Microsoft.SharePoint.PowerShell" 
     }
     catch
     {
         if($Error[0].Exception.Message.Contains("No Windows PowerShell snap-ins matching the pattern 'Microsoft.SharePoint.PowerShell' were found"))
         {
             Add-PSSnapin "Microsoft.SharePoint.PowerShell"
         }
     }
     Write-Host "Finished Adding PowerShell Snap-in" -ForegroundColor Green
 }
 
  
 
  
  function WaitForTimerJobToFinish([string] $solutionName){
 	$jobname = "*solution-deployment*$solutionName*"
 	$job = Get-SpTimerJob | ?{ $_.Name -like $jobname }
 	if($job -eq $null){
 		write-host "timer job not found"
 	}
 	else {
 		$jobfullname = $job.Name
 		write-host -NoNewLine "waiting to finish job $jobfullname.."
 		while((Get-SPTimerJob $jobfullname) -ne $null){
 			write-host -NoNewLine .
 			Start-Sleep -Seconds 2
 		}
 		write-host "=|="
 	}
 }
 
 function Add-SandboxSolution([System.Xml.XmlElement]$solutionXml,[string]$solutionsFolder , [bool]$includeFeatures , [string]$siteurl) {
 Write-Host "deploying a sandbox solution $($solutionXml.Name) to $($siteurl)"
 	if($solutionsFolder -eq $null){
 		Write-Host "No solutions folder defined"
 		return
 	}
 	$path = $global:currPath + $solutionsFolder + "\" + $solutionXml.Name + ".wsp"
 	$url = $solutionXml.Target
 	if($url -eq "this"){
 		$url = $global:thisUrl
 	}
 	if($url -eq "root"){
 		$url = $global:thisIISUrl
 		$includeFeatures = $true
 	}	
 	if($url -eq "site"){
 		$url = $siteurl
 	}	
 	
 	if($solutionXml.Sandbox -eq "True"){
 		Write-Host "deploying to target $($url)"
 	if($solutionXml.Name.length -gt 0){
 		$solutionFileName = $solutionXml.Name + ".wsp"
 		$solutionFileOldName = $solutionXml.OldName + ".wsp"
 		$solution = Get-SPUserSolution -Identity $solutionFileName -Site $url   -ErrorAction SilentlyContinue	
 		if($solutionXml.OldName -ne $null){
 			$oldsolution = Get-SPUserSolution -Identity $solutionFileOldName -Site $url   -ErrorAction SilentlyContinue	
 			write-host "Found solution $($oldsolution.Name) Guid = $($oldsolution.SolutionId) status = $($oldsolution.Status)"
 		}
 		if($solution -ne $null){
 			write-host "Found solution $($solution.Name) Guid = $($solution.SolutionId)  status = $($solution.Status)"	
 			if(($solutionXml.RemoveIfExists -eq "True") -or ($solutionXml.Upgrade -eq "False") ){	
 				Write-Host "removing sandbox solution "
 				Remove-SPUserSolution -Identity $solutionFileName -Site $url
 			}
 		}
 		Write-Host "adding sandbox solution from $($path)"
 		Add-SPUserSolution -LiteralPath $path -Site $url
 		
 		$solution = Get-SPUserSolution -Identity $solutionFileName -Site $url  
 		write-host "Successfully added solution $($solution.Name) Guid = $($solution.SolutionId)"
 	
 		if($solution -ne $null){
 			if($solutionXml.Target -ne $null){
 				if($solutionXml.Upgrade -eq "True"){		
 					if($oldsolution -ne $null){
 						write-host "Upgrading from solution $($solution.OldName) to $($solution.Name)"
 						Update-SPUserSolution -Identity $solutionFileOldName -ToSolution $solutionFileName -Site $url 
 					}
 					else{
 						write-host "COuld not open previous solution $($solution.OldName) "
 					}
 				}
 				else{
 					Write-Host "installing sandbox solution to $($url)"
 					Install-SPUserSolution -Identity $solution.Name -Site $url
 					WaitForTimerJobToFinish $solution.Name
 					if($includeFeatures -eq $true -and $url -ne $null){
 						$solutionXml.Features.Feature | ForEach-Object {
 							write-host "activating sandbox feature $($_.Name) in $($url)"
 							$x = Enable-SPFeature -Identity $_.Name -url $url -Force
 						}
 					}
 				}
 			}
 			else{
 					Write-Host "solution target not specified"
 			}
 		}
 	}
 	else{
 		if($includeFeatures -eq $true -and $url -ne $null){
 			$solutionXml.Features.Feature | ForEach-Object {
 				Add-Feature $_  $url
 			}
 		}
 	}
 	write-host "Finished deploying solution"
 	}
 	else{
 	write-host "Not a sandbox solution"
 	}
 }
 
 
 try
 {
     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint")
     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint.Administration")
 
     $ConfigXmlPath = $scriptBase + "\..\ConfigXML\DeployConfiguration.xml"
     Write-Host "Read the Config Values" -ForegroundColor Green 
     [Xml]$Config = Get-Content $ConfigXmlPath  
     
     AddPowerShellSnapin
 
     $solutionPath = $scriptBase + "\..\SandBoxSolutions"
 
     $Config.Configuration.Solutions.Solution | Where-Object {$_.Sandbox -eq "True" } | ForEach-Object{
 	Add-SandboxSolution $_ $solutionPath $false $Config.Configuration.PublishingSite.URL
 
  }
 
 }
 catch
 {
     Write-Host "Custom Exception Happened on Main : " + $Error[0].Exception.Message -ForegroundColor Red  
 }
 

And the config file as below.

 <Configuration EnvironmentName="Dev5">
   <PublishingSite URL="http://spdev5.dev.cbre.eu/sites/CTSSchema" />
   <Solutions>
       <Solution Name="MYWSPName" Target="site" Sandbox="True"  RemoveIfExists="True" Upgrade="False" OldName=" MYWSPName "/>
     </Solutions>
   
    
 </Configuration>
 

Happy Coding,

Sathish Nadarajan.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

Migratiin Tools for SharePoint