Saturday, October 20, 2012

How to Deploy wsp in SharePoint 2010 using PowerShell

Overview
In SharePoint 2010, STSADM command is supported but PowerShell commands are recommended to use. PowerShell command is powerful and extensible. It is not case sensitive but i suggest to use Caml Case format for good practice. It is used to deploy solutions/features in SharePoint 2010 environment.

Format of the PowerShell command
Basically Powershell coammands are built with Verb, Hyphen -) and Noun syntax. So Powershell command has two parts which are:
  1. Verb
  2. Noun
Example: Execute below command in Powershell window to add the wsp solution file
              Add-SPSolution "D:\\Filename.wsp"
Here, Add is a Verb and SPSolution is a Noun with Hyphen(-). i hope that you can understand it.

Solution Deployment
There are eight steps involved in SharePoint 2010 deployment those are:
  1. Deactivate Features
  2. Uninstall Features
  3. Retract Solution
  4. Delete Solution
  5. Add Solution
  6. Deploy Solution
  7. Install Features
  8. Activate Features
Example:  Change solution name , SolutionPath and HostName.

$solutionName="TestWP.wsp"
$SolutionPath="D:\\WebParts\TestWP.wsp"
Write-Host 'Going to disable feature'
disable-spfeature -identity TestWPFeature -confirm:$false -url http://HostName:1111
Write-Host 'Going to uninstall feature'
uninstall-spfeature -identity TestWPFeature -confirm:$false -force
Write-Host 'Going to uninstall solution'
Uninstall-SPSolution -identity $solutionName  -allwebapplications -confirm:$false

Write-Host 'Going to remove solution'
Remove-SPSolution –entity $solutionName -confirm:$false
Write-Host 'Going to add solution'
Add-SPSolution $SolutionPath
Write-Host 'Going to install solution to all web applications'
Install-SPSolution –entity $solutionName –Allwebapplications –GACDeployment

Write-Host 'Going to enable Feature' 
Enable-spfeature -identity TestWPFeature -confirm:$false -url http://HostName:1111 

Save above script as a filename "deploySolutionScript.ps1" and save below script as bat file named "RunDeployment.bat" then double click on bat file which will deploy the solution.
cd /d %~dp0
powershell -noexit -file    "\DeploySolutionScript.ps1" "%CD%"

Note: Make sure that all the files should be placed inside a folder.

Conclusion
Powershell script is easy way to deploy your custom solutions/Features into SharePoint platform. Powershell also supports SharePoint Programming features. So you can do all kind of SharePoint programs. Powershell script's Basic knowledge must for SharePoint developers.