Forum Discussion
Praveen_KumarS
Oct 01, 2024Copper Contributor
How to do deployment of WSP Solution using PNP Powershell in SharePoint
Looking for sample code details for below mentioned using PNP PowerShell.
-Uploading WSP file to Solution gallery of Classic SharePoint site
-Installing Solution
-Activating the solution
-Apply the custom template to SharePoint site.
#Below is CSOM based Code - looking for code in PNP Based
$fileBytes =[System.IO.File]::ReadAllBytes("D:\CustomTemplate.wsp")
$fileCreateInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$fileCreateInfo.Content = $fileBytes
$fileCreateInfo.Url = $list.RootFolder.ServerRelativeUrl + "/CustomTemplate.wsp"
$fileCreateInfo.Overwrite = $true
$file = $list.RootFolder.Files.Add($fileCreateInfo)
$Ctx.Load($file)
$Ctx.ExecuteQuery()
$designPackageInfo = New-Object Microsoft.SharePoint.Client.Publishing.DesignPackageInfo
$designPackageInfo.PackageName = "CustomTemplate.wsp"
$WSP = New-Object Microsoft.SharePoint.Client.Publishing.DesignPackageInfo
$WSP.PackageGuid = [System.Guid]::Empty
$WSP.PackageName = "CustomTemplate.wsp"
$WSP.MajorVersion = 1
$WSP.MinorVersion = 0
$WSPFileURL = $list.RootFolder.ServerRelativeUrl + "/" + "CustomTemplate.wsp";
[Microsoft.SharePoint.Client.Publishing.DesignPackage]::Install($Ctx, $Ctx.Site, $WSP, $WSPFileURL)
$Ctx.ExecuteQuery()
Write-Host -f Green "`tInstalled the Solution Successfully!"
#--------------------------------------------------------------------------------
[Microsoft.SharePoint.Client.Publishing.DesignPackage]::Install($Ctx, $site, $designPackageInfo, $fileCreateInfo.Url)
$Ctx.ExecuteQuery()
# Below is the main code to activate template and assign home page in site.
[Microsoft.SharePoint.Client.Publishing.DesignPackage]::Apply($Ctx, $Ctx.Site, $WSP)
$Ctx.ExecuteQuery()
$web = $Ctx.Site.RootWeb
$templateName = “{E7ED6200-07BF-42F8-94CB-F6560D080DFA}#SZ"
$web.ApplyWebTemplate($templateName)
$web.update()
$Ctx.ExecuteQuery()
- Praveen_KumarSCopper Contributor
tried using the solution id, but no luck.
- Praveen_KumarSCopper Contributor
Thank you very much for your response. I am getting error solution not found at installation of solution step. tried various ways to get the items in solution gallery and use guid / name/ id but gives error solution not found. is there any settings/property to be enabled on site collection ?
WSP exist in solution gallery, I am able to manually activate the solution but unable to install using PNP.
- kyazaferrSteel Contributor
# Connect to the SharePoint site
Connect-PnPOnline -Url "https://yoursharepointsite" -UseWebLogin# Upload WSP file to the Solution Gallery
$wspFilePath = "D:\CustomTemplate.wsp"
$wspFileName = "CustomTemplate.wsp"# Upload the WSP file to the Solution Gallery
Add-PnPFile -Path $wspFilePath -Folder "Solutions" -OverwriteWrite-Host "WSP File uploaded successfully to Solution Gallery."
# Install the Solution
# Installing the solution
Install-PnPApp -Identity $wspFileNameWrite-Host "Solution installed successfully."
# Activate the Solution
# Activating the solution
Enable-PnPApp -Identity $wspFileNameWrite-Host "Solution activated successfully."
# Apply Custom Template to SharePoint Site
# The template GUID (change it to the actual template GUID)
$templateGUID = "{E7ED6200-07BF-42F8-94CB-F6560D080DFA}#SZ"# Apply the template to the site
Set-PnPWeb -Template $templateGUIDWrite-Host "Custom template applied successfully to the site."
- kyazaferrSteel Contributor
- Upload WSP to the Solution Gallery
- Install the Solution
- Activate the Solution
- Apply the Custom Template