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".
  • In the site setting page, Click on "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. upload solution package in sharepoint online
  • Browse and select the WSP file from your local disk and Upload solution.sharepoint online design package deployment
  • 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.activate design package in sharepoint online
  • Finally the solution will be activated in your SharePoint Online site! How to Deploy a Design Package Solution (WSP) in sharepoint online using PowerShell

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 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
SharePoint Online: How to Deploy a Design Package Solution (WSP) using PowerShell? SharePoint Online: How to Deploy a Design Package Solution (WSP) using PowerShell? Reviewed by Salaudeen Rajack on December 30, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.