欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

通过PowerShell(命令行)来上传并**solution(.wsp)到SharePoint online环境

程序员文章站 2022-04-05 19:05:23
...

大家都知道,在SharePoint on-prem 环境中我们可以通过PowerShell来上传solution(.wsp)文件到自己的SharePoint并且**它。但是如何在SharePoint online的环境中使用powershell来做呢? 因为我们接触不到online环境的机器,也就不能在online的机器上面使用powershell。如果你认为就不能通过powershell来实现了的话,那你就太小看windows的powershell了。

因为SharePoint为我们提供了CSOM,即客户端对象模型,所以我们可以通过PowerShell使用客户端对象模型来做。下面我就为大家展示如何操作。

首先需要打开powershell,然后再命令行中导入我们所需的dll文件:

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' 

引入完dll文件,那么我们接着就在命令行中写剩下的Client Object Model:

$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()

下面是我运行的结果,供参考:

通过PowerShell(命令行)来上传并**solution(.wsp)到SharePoint online环境


本人原创,如有不正之处,还望各位大佬指正!