How to Deploy a SharePoint 2013 Solution (WSP) in the Farm using PowerShell


Sathish Nadarajan
SharePoint MVP
Published On :   28 Dec 2013
Visit Count
Today :  12    Total :   32791
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!


All of us will be using Visual Studio as our Development IDE for our development purpose. Once our development completes, Visual Studio itself prepares the WSP for us. We are delivering the WSP as a deliverable. Later on moving the WSP to a Staging, we require some sort of deployment procedure. For that, I would say PowerShell is the best Option. Let us, how to write a PowerShell Snippet to Deploy our WSP in Farm level as well as Web Application Level.

First we will see, how to deploy our WSP against a particular Web Application. The script is self-explanatory and commented with the steps.

Before, seeing the script, let us see the sequence of the script.

1. Get the Site, WebApplication URL, Solution Name, Solution Location.

2. Check whether the Solution is already installed.

3. If installed, then find, whether it is deployed in any of the WebApplication.

4. If deployed, then retract from all the web applications already deployed.

5. Then un-install the wsp.

6. Add the WSP.

7. Install the WSP against the WebApplication, which we are specifying.

This gives a proper validation for the deployment script. Now, let us see the script.

 Add-PSSnapin "Microsoft.SharePoint.PowerShell" 
 
 function wait4timer($solutionName) 
 {    
     $solution = Get-SPSolution | where-object {$_.Name -eq $solutionName}    
     if ($solution -ne $null)     
     {        
         Write-Host "Waiting to finish soultion timer job" -ForegroundColor Green      
         while ($solution.JobExists -eq $true )          
         {               
             Write-Host "Please wait...Either a Retraction/Deployment is happening" -ForegroundColor DarkYellow           
             sleep 2            
         }                
 
         Write-Host "Finished the solution timer job" -ForegroundColor Green  
         
     }
 }  
 
       
     try
     {
         # Get the WebApplicationURL
         $MyWebApplicationUrl = "http://WEBAPPLICATIONURL";
         
         # Get the Solution Name
         $MywspName = "MySolution.WSP"
         
         # Get the Path of the Solution
         $MywspFullPath = "D:\Test\MySolution.WSP"
 
         # Try to get the Installed Solutions on the Farm.
         $MyInstalledSolution = Get-SPSolution | Where-Object Name -eq $MywspName
         
         # Verify whether the Solution is installed on the Target Web Application
         if($MyInstalledSolution -ne $null)
         {
             if($MyInstalledSolution.DeployedWebApplications.Count -gt 0)
             {
                 wait4timer($MywspName)  
 
                 # Solution is installed in atleast one WebApplication.  Hence, uninstall from all the web applications.
                 # We need to unInstall from all the WebApplicaiton.  If not, it will throw error while Removing the solution
                 Uninstall-SPSolution $MywspName  -AllWebApplications:$true -confirm:$false
 
                 # Wait till the Timer jobs to Complete
                 wait4timer($MywspName)   
 
                 Write-Host "Remove the Solution from the Farm" -ForegroundColor Green 
                 # Remove the Solution from the Farm
                 Remove-SPSolution $MywspName -Confirm:$false 
 
                 sleep 3
             }
             else
             {
                 wait4timer($MywspName) 
 
                 # Solution not deployed on any of the Web Application.  Go ahead and Remove the Solution from the Farm
                 Remove-SPSolution $MywspName -Confirm:$false 
 
                 sleep 3
             }
         }
 
         wait4timer($MywspName) 
 
         # Add Solution to the Farm
         Add-SPSolution -LiteralPath "$MywspFullPath"
     
         # Install Solution to the WebApplication
         install-spsolution -Identity $MywspName -WebApplication $MyWebApplicationUrl -FullTrustBinDeployment:$true -GACDeployment:$false -Force:$true
 
         # Let the Timer Jobs get finishes       
         wait4timer($MywspName)    
 
         Write-Host "Successfully Deployed to the WebApplication" -ForegroundColor Green 
         
     }
     catch
     {
         Write-Host "Exception Occuerd on DeployWSP : " $Error[0].Exception.Message -ForegroundColor Red  
     }
 

This will install against a particular WebApplication and the deployment is happening against the Bin Folder.

The Tags,

 -FullTrustBinDeployment:$true -GACDeployment:$false
 

Decides about the deployment type. If we want to do a GAC Deployment, then we need to give

 -FullTrustBinDeployment:$false -GACDeployment:$true

This will deploy against a particular Web Application. Some of the WSPs needs to be deployed globally. Let us see, how to do that and what are the tags required for them.

 install-spsolution -Identity $wsp –AllWebApplications:$true

This will deploy our WSP on all web applications.

Happy Coding.

Sathish Nadarajan.

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

KWizCom Scan