Upload and activate solution(.wsp) to the SharePoint online environment through PowerShell (command line)

Keywords: shell Web Server Windows

As you all know, in SharePoint on-prem environment, we can upload the solution(.wsp) file to our SharePoint through PowerShell and activate it. But how to use power shell in SharePoint online environment? Because we can't touch the machine in online environment, we can't use power shell on the machine in online environment. If you think you can't do it through Powershell, you're underestimating windows's Powershell.

Because SharePoint provides us with CSOM, the client object model, we can use the client object model through PowerShell. Now I'll show you how to operate.

First you need to open the power shell, and then import the dll file we need from the command line:

Import-Module 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll' 
Import-Module 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll' 
Import-Module 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Publishing.dll' 

After introducing the dll file, we then write the remaining Client Object Model on the command line:

$url ="Your Site URL" 
$username="UserName" 
$password="Password" 
$Password = $password |ConvertTo-SecureString -AsPlainText -force 
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($url) 
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password) 
$Context.Credentials = $credentials 
$context.RequestTimeOut = 5000 * 60 * 10; 
$web = $context.Web 
$site = $context.Site 
$list = $context.Web.Lists.GetByTitle('Solution Gallery') 
$context.Load($web) 
$context.Load($site) 
$context.Load($list) 
$context.Load($list.RootFolder) 
$context.ExecuteQuery() 
$fileBytes = [System.IO.File]::ReadAllBytes("C:\MyCustomSiteTemplate.wsp") 
$fileCreateInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation 
$fileCreateInfo.Content = $fileBytes 
$fileCreateInfo.Url = $list.RootFolder.ServerRelativeUrl + "/MyCustomSiteTemplate.wsp" 
$fileCreateInfo.Overwrite = $true 
$file = $list.RootFolder.Files.Add($fileCreateInfo) 
$context.Load($file) 
$context.ExecuteQuery() 

$designPackageInfo = New-Object Microsoft.SharePoint.Client.Publishing.DesignPackageInfo 
$designPackageInfo.PackageName = "MyCustomSiteTemplate" 
[Microsoft.SharePoint.Client.Publishing.DesignPackage]::Install($context, $site, $designPackageInfo, $fileCreateInfo.Url) 
$context.ExecuteQuery()

The following is the result of my operation for reference:



I'm original, if there are any shortcomings, I hope you guys correct!

Posted by Ansel_Tk1 on Tue, 12 Feb 2019 05:54:18 -0800