SharePoint Online: How to Deploy a Design Package Solution (WSP) using PowerShell?
Requirement: Deploy a design package solution (WSP) in SharePoint Online.
Prerequisites: You must activate publishing infrastructure feature both at site collection and site levels before deploying any design package!
How to deploy a Solution Package using PowerShell?
You may have a solution package WSP file for site template or design package for branding, or some other sandbox solution. Here is how the SharePoint Online Solution Deployment works:
Upload the WSP File to Solution Gallery and Activate the solution:
Use the following steps to deploy a WSP solution in SharePoint Online.
- Log in as a Site Collection Administrator >> Navigate to your SharePoint Online Site Collection (Root Site) >> Click on “Site settings”.
- On the site setting page, click on the “Solutions” link under the “Web Designer Galleries” section. This opens the solution page as below screen. On the solutions page, Click on the “upload solution” button from the ribbon.
- Browse and select the WSP file from your local disk and Upload the solution.
- After clicking on the “OK” button, SharePoint Uploads the WSP file and presents you a page from which you can activate the solution, as shown below. It takes a while to activate the solution.
- Finally, the solution will be activated in your SharePoint Online site!
PowerShell to deploy a Solution Package in SharePoint Online:
This PowerShell script deploys the given solution package and activates the solution.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Function Deploy-SPOSolution()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $WSPFilePath,
[Parameter(Mandatory=$False)] [Int] $MajorVersion = 1,
[Parameter(Mandatory=$False)] [Int] $MinorVersion = 0
)
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
#Get the Solution Gallery
Write-Host -f Yellow "Uploading WSP File to Solution Gallery..."
$SolutionGallery = $Ctx.Web.Lists.GetByTitle("Solution Gallery")
$SolutionGalleryRootFolder = $SolutionGallery.RootFolder
$Ctx.Load($SolutionGallery)
$Ctx.Load($SolutionGallery.RootFolder)
$Ctx.ExecuteQuery()
#Get WSP File Name from Path
$WSPFileName = $WSPFilePath.Substring($WSPFilePath.LastIndexOf("\")+1)
#Upload the WSP to the solution gallery
$WSPFileStream = New-Object System.IO.FileStream($WSPFilePath, [System.IO.FileMode]::Open)
$FileCI = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCI.Overwrite = $True
$FileCI.ContentStream = $WSPFileStream
$FileCI.URL = $WSPFileName
$UploadedFile = $SolutionGallery.RootFolder.Files.Add($FileCI)
$Ctx.Load($UploadedFile)
$Ctx.ExecuteQuery()
Write-Host -f Green "`tUploaded WSP File to Solution Gallery!"
#Install the solution
Write-Host -f Yellow "Installing Solution..."
$WSP = New-Object Microsoft.SharePoint.Client.Publishing.DesignPackageInfo
$WSP.PackageGuid = [System.Guid]::Empty
$WSP.PackageName = $WSPFileName
$WSP.MajorVersion = $MajorVersion
$WSP.MinorVersion = $MinorVersion
#Install the solution from the file url - This creates a solution according to the Major and Minor Version
$WSPFileURL = $SolutionGallery.RootFolder.ServerRelativeUrl + "/" + $WspFileName;
[Microsoft.SharePoint.Client.Publishing.DesignPackage]::Install($Ctx, $Ctx.Site, $WSP, $WSPFileURL)
$Ctx.ExecuteQuery()
Write-Host -f Green "`tInstalled the Solution Successfully!"
#Activate the solution
Write-Host -f Yellow "Activating the Solution..."
[Microsoft.SharePoint.Client.Publishing.DesignPackage]::Apply($Ctx, $Ctx.Site, $WSP)
$Ctx.ExecuteQuery()
Write-Host -f Green "`tActivated the Solution Successfully!"
#Remove the original wsp file uploaded
$UploadedSolutionFile = $SolutionGallery.rootFolder.Files.GetByUrl($WSPFileURL)
$UploadedSolutionFile.DeleteObject();
$ctx.ExecuteQuery()
Write-Host -f Green "`n*** WSP Deployment has been successfully completed!***"
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
}
#Set Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/creditpipeline"
$WSPFilePath = "C:\Users\salaudeen\Desktop\Crescent-Projects-v2.wsp"
#Get Credentials to connect
$Cred = Get-Credential
#call the function to deploy the solution
Deploy-SPOSolution -SiteURL $SiteURL -WSPFilePath $WspFilePath
Hi Salaudeen,
Great script.
For me this fails on step 3: Activating the Solution.
1) To Fix this i had to download the below Dll and add a path to this in the beginning of the script.
Microsoft.SharePoint.Client.Publishing.dll
2) I had to enable “SharePoint Server Publishing Infrastructure” in Site Collection Features.
Otherwise the script works great.
Kudos.
Floyd