Sunday, October 16, 2016

SharePoint Solution (WSP) management using PowerShell

My recommended approach to manage SharePoint WSP solutions is; Powershell instead of GUI approach using SharePoint administration. Despite that, you can use the Central Administration to do that.
Open SharePoint Powershell or windows Powershell using ‘Run as administrator’.
If you open windows default Powershell , you need to load the SharePoint snap in explicitly, it auto loads in case of SharePoint Powershell.

PS C:\Windows\system32> add-pssnapin Microsoft.Sharepoint.Powershell

We will see some important Powershell cmdlet which are useful for solution management. (This article will be mainly focused on main parameters available with cmdlets, for all parameter details, please refer to Ms technet


Add-SpSolution -LiteralPath [path of the wsp file with name and extension]

Add a SharePoint WSP solution to the Farm.

Build your solution in visual studio, ->Publish to a folder. You will find a WSP file named with your solution name. For example, it is ‘MyCore.Aritifacts.wsp’.

Let’s add this solution.

Please check the solution has been added but not deployed yet.


This cmdlet deploys the installed sharepoint solution to the farm.

PS> Install-SPSolution -Identity nameOfTheSolution.wsp -GACDeployment -CompatibilityLevel 15
PS> Install-SPSolution -Identity nameOfTheSolution.wsp -GACDeployment -CompatibilityLevel {14,15}

This deploys solution to the farm and tells for the GAC deployment if required.
-CompatibilityLevel parameter is optional and following are the values which can be used with that.

Installs solution to 14 directories only
Installs solution to 15 directories only
Installs solution to both 14 and 15 directories
“AllVersions” or “All”
Installs solution to both 14 and 15 directories
“OldVersions” or “Old”
Installs solution to 14 directories only
“NewVersion” or “New”
Installs solution to 15 directories only

This deploys the solution to the farm, please check the snap shot of the SharePoint central admin- > solution management.

It is globally deployed. It means deployed to all the servers in the Farm. You can now activate the feature at Site Collection level depending on the scope of the feature.

Before you remove any deployed solution from the farm, you need to make sure that it has been retracted from the farm.

To retract the deployed solution, Remove-SPSolution is used as following. This is like preparation to completely uninstall a solution from entire SP Farm.

PS> Uninstall-SPSolution -Identity nameOfTheSolution.wsp

You can use the CompatabilityLevel parameter like we used in Install-SPSolution.

Cmdlet will ask if you are sure to complete the operation. Type “Y” if you are share to uninstall the solution.
The Admin panel shall look like following now.
This says the solution is not yet completely retracted.
After some time, it shall look like the following.
Solution “Not Deployed”.
Now are are ready to remove the solution completely from the Farm.

This command removed the solution from the SP Farm.

PS> Remove-SPSolution -Identity nameOfTheSolution.wps

This will again ask you for the confirmation, press “Y” if you are sure to delete. Once you press “Y”. This SP will start the removal of Solution from the Farm.

Check with in few seconds (depending on the size of the solution), refresh the solution management page in Administration and you will find the solution no more exists in the Farm.

Before I end the article there are some very crucial tips which you can use with the above command.
The Powershell in some scenarios will not allow your operations, for example, if the solution is being deployed on the farm, your uninstall command will not be accepted. If the solution is being retracted from the farm, you remove solution command will not be accepted. For real scenarios, I would encourage you to handle all these exceptions using the Powershell commands.

I am trying to install my solution on the Farm

Now immediately I tried to uninstall the Solution. Following is the screenshot of the error.

This throws an exception describing 'the solution can’t be retracted at the moment as it already under deployment or retraction'. In our case it is under deployment. In the same way if the solution is being retracted and you are trying to remove it shall through similar exception. I recommend to handle this in Powershell script like following. The screenshot below checks it the solution exists and is deployed to the Farm. After this verification only it commands to uninstall the Solution. Below the screenshot, I will provide the cmdlet as well.

PS> $spSolName = "yourSolutionName.wsp"
PS> $mySol = Get-SPSolution | where-object { $_.Name -eq $spSolName}
PS> $mySol

Name                           SolutionId                           Deployed
----                           ----------                           --------
yourSolutionName.wsp        797a6dd5-352c-418a-8ec6-2fb55c6740d7 True

PS> if($mySol -ne $null) { if($mySol.Deployed -eq $true) { uninstall-s
psolution -identity $mySol  }  }

Are you sure you want to perform this action?
Performing the operation "Uninstall-SPSolution" on target "yourSolutionName.wsp".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help
(default is "Y"):Y

I am sure, now you have an idea how powerfully we can use the Powershell cmdlet for the basic operation. I would encourage you to prepare the script file for these operations. This was just an example to handle exception during uninstall, you can similarly handle exception for remove and other cmdlets.
You can similarly add a while loop in powershell to wait unless the Solution is ready to be installed/uninstalled etc.

while ($mySol.Deployed -eq $true)
           Write-Host -NoNewLine
           Start-Sleep -Seconds 2

I simply used the command in my already opened prompt (where I already have values for variable stored).
It just stopped after the first line (sleeps for 2 seconds before looping). Then I manually started the retraction of the solution from the Solution Management in Admin Panel. Once the solution status came to “Not Deployed”, then the command finished and went to the next line. Now you can understand how crucial are these cmdlets during Live scenarios of Solution Management in a Farm.

Keep Learning…...

Visual Studio Build Error - The "GenerateResource" task failed unexpectedly

Error: "The GenerateResource task failed unexpectedly" This error is quite common while building the project in Visual Studio....