About Me

Having 12 years experience in Microsoft technologies.Since more than 7 years working in SharePoint technologies. Expert in providing consultation for SharePoint projects. Hands on with development and administration.

Wednesday 19 December 2012

PowerShell script to deploy Sandbox solution

Applies to SPFoundation 2010, SP 2010, SPFoundation 2013,SP 2013

In general we will write a script to deploy farm solutions. But we will upload sandbox solutions into site collection solution gallery. What if, we want to automate the process to deploy sandbox solution without uploading.... here is the script

Points to be noted:
1. The .wsp should be in the current directory of the script (.ps1)
2. Replace $solutionName with your solution name.
3. Replace $SiteURL with your site collection url.

Add-PsSnapin Microsoft.SharePoint.PowerShell
Write-Host 'Poweshell Script will initialize parameters'
$CurrentDir=$args[0]
$solutionName="xxxx.wsp"
$SolutionPath=$CurrentDir + "\"+$solutionName
$logfile=$CurrentDir + "\log.log"
$SiteURL = "xxxxxxx"

Start-Transcript $logfile
$errorActionPreference = 'Inquire'

$solution = Get-SPUserSolution -site $siteURL | Where { ($solutionName -eq $_.Name) }  -ErrorAction SilentlyContinue 
try
{
if ($solution -ne $null)
{
if($solution.Status -eq "Activated")
{
Write-Host 'Poweshell Script will now retract solution:' $solutionName

Uninstall-SPUserSolution -Identity $solutionName -Site $SiteURL -Confirm:$false
Write-Host 'Successfully retracted solution:' $solutionName
}
Write-Host 'Removing Solution:' $solutionName
Remove-SPUserSolution -Identity $solutionName -Site $siteURL -Confirm:$false
Write-Host 'Successfully removed Solution:' $solutionName
}

Write-Host 'Poweshell Script will now add solution:' $solutionName
Add-SPUserSolution –LiteralPath $SolutionPath -Site $SiteURL
Write-Host 'Poweshell Script will now deploy solution:' $solutionName
Install-SPUserSolution -Identity $solutionName -Site $SiteURL
Write-Host 'Poweshell Script has finished deployment of solution:' $solutionName
}

catch [system.Exception]
{
Write-Error 'An Unknown error occured while trying to deploy/redeploying the solution:' $_.Exception.Message
}


Stop-Transcript
Remove-PsSnapin Microsoft.SharePoint.PowerShell

Happy powershell programming......

1 comment: