Managing Solution Packages Using PowerShell

Windows PowerShell comes in handy for installing as well as removing a solution from a SharePoint farm. It’s relatively easy to do and can save you time. Let’s walk through how it’s done.

Installation

You can install a solution package by loading Microsoft.SharePoint.Powershell snap-in and then calling the Add-SPSolution cmdlet passing the –LiteralPath parameter using a value that contains the physical path to the solution package file.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction
  "SilentlyContinue"
Add-SPSolution -LiteralPath "C:\Solutions\Wingtip
  DevProject1.wsp"

Once you have added the solution package using the Add-SPSolution cmdlet, you can deploy it using the Install-SPSolution cmdlet. Note that once the solution package has been added, you refer to it using the –Identity parameter whose value should be the name of the solution package file without any path.

Add-PSSnapin Microsoft.SharePoint.PowerShell
  -ErrorAction"SilentlyContinue"
Add-SPSolution -LiteralPath "C:\Solutions\Wingtip
  DevProject1.wsp"
Install-SPSolution -Identity "WingtipDevProject1.wsp"
  -Local -GACDeployment

You should also observe that the call to Install-SPSolution includes two additional parameters which are –Local and –GACDeployment. The –Local parameter tells SharePoint Foundation that it only needs to worry about deploying the solution package on the local server which can speed things up when developing on a single-server farm. The other parameter –GACDeployment is required whenever you are deploying a solution package which installs an assembly in the GAC.

Retracting and Removing a Solution

If you want to remove a solution package from a farm after it has been deployed you must retract it and then remove it. The act of retracting reverses the act of deployment. For example, retracting a solution will force SharePoint Foundation to delete all the files it copied during deployment as well as uninstalling features and deleting assemblies from the GAC. Once you have retracted a solution you can then remove it which deletes the solution package file from the configuration database.

You can retract a solution package using the Uninstall-SPSolution cmdlet. When calling Uninstall-SPSolution you should pass the -Identity parameter and the -Local parameter in the same manner as when calling Install-SPSolution. You should also pass the –Confirm parameter with a value of $false because failing to do so can cause the cmdlet to prompt the user. This can cause problems because it has the effect of freezing Visual Studio when you execute a script that prompts the user for a response.

Add-PSSnapin Microsoft.SharePoint.PowerShell
  -ErrorAction "SilentlyContinue"
$SolutionPackageName = "WingtipDevProject1.wsp"
Uninstall-SPSolution -Identity $SolutionPackageName
  -Local -Confirm:$false

Once you have retracted the solution using the Uninstall-SPSolution cmdlet, you can then remove it by calling Remove-SPSolution which instructs SharePoint Foundation to delete the solution package file from the configuration database.

Add-PSSnapin Microsoft.SharePoint.PowerShell
  -ErrorAction "SilentlyContinue"
$SolutionPackageName = "WingtipDevProject1.wsp"
Uninstall-SPSolution -Identity $SolutionPackageName
  -Local -Confirm:$false
Remove-SPSolution -Identity $SolutionPackageName
  -Confirm:$false

Of course, these calls to Uninstall-SPSolution and Remove-SPSolution will fail if the solution package is not currently installed and deployed. Therefore, it makes sense to add a call to Get-SPSolution and conditional logic to determine whether the solution package is currently installed and deployed before attempting to retract it or remove it.

Add-PSSnapin Microsoft.SharePoint.PowerShell
  -ErrorAction "SilentlyContinue"
$SolutionPackageName = "WingtipDevProject1.wsp"
$solution = Get-SPSolution | where-object \\{$_.Name
  -eq $SolutionPackageName\\}
# check to see if solution package has been installed
if ($solution -ne $null) \\{
# check to see if solution package is currently deployed
if($solution.Deployed -eq $true)\\{
Uninstall-SPSolution -Identity $SolutionPackageName
  -Local -Confirm:$false
\\}
\\}
Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish