SharePoint Pals
 | Sign In
How to Deploy the SandBox Solutions in SharePoint 2013 Using PowerShell
by Sathish Nadarajan 6 Dec 2015
Author
SharePoint MVP
Visits   
Today  :   3     Total  :    5594

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.

blog comments powered by Disqus

SharePoint Pals

Pals
SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile

Training

Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community




SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to articles@sharepointpals.com in a word document.

Related Resources

Recent Tweets

Twitter January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart https://t.co/VxSi4QnNrC

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window https://t.co/EQ7HkoZDkX

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features https://t.co/zAXObHCFpH

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013 https://t.co/LEwnUoI7EY

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services https://t.co/Yvp2VPFIRP

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation