通过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()
下面是我运行的结果,供参考:
本人原创,如有不正之处,还望各位大佬指正!
因为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()
下面是我运行的结果,供参考:
本人原创,如有不正之处,还望各位大佬指正!
相关文章推荐
- Sharepoint Solution Gallery Active Solution时激活按钮灰色不可用的解决方法
- SharePoint自动化系列——通过PowerShell创建SharePoint Web
- SharePoint自动化系列——通过PowerShell创建SharePoint List Items
- SharePoint online powershell
- Deploy WSP with SharePoint 2010 PowerShell
- SharePoint solution and feature management with PowerShell
- SharePoint PowerShell命令系列 (3) 操作WSP解决方案包的相关命令
- SharePoint自动化系列——通过PowerShell创建SharePoint Lists
- Sharepoint Service Error: A deployment or retraction is already under way for the solution "xxx.wsp" , and only one deployment or retraction at a time
- Office 365 - Windows PowerShell for SharePoint Online
- SharePoint solution and feature management with PowerShell
- SharePoint solution and feature management with PowerShell
- SharePoint自动化系列——通过PowerShell创建SharePoint Site Collection
- Sharepoint Solution Gallery Active Solution时激活按钮灰色不可用的解决方法
- 小议:如何保存SharePoint farm中的.WSP Solution?
- Sharepoint Solution Gallery Active Solution时激活按钮灰色不可用的解决方法
- 如何通过flow修正SharePoint Online时差问题
- PowerShell 方式部署Sharepoint Solution
- Sharepoint Solution Gallery Active Solution时激活按钮灰色不可用的解决方法
- How to Deploy Solution (WSP) from SharePoint 2010 Central Administration