您的位置:首页 > 运维架构 > Shell

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

2017-09-30 23:27 609 查看
大家都知道,在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()

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



本人原创,如有不正之处,还望各位大佬指正!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: