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

大家都知道,在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()

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



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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值