【问题标题】:How to connect to SharePoint 2013 on-premise using CSOM in PowerShell如何在 PowerShell 中使用 CSOM 连接到本地 SharePoint 2013
【发布时间】:2017-09-16 13:50:30
【问题描述】:

我知道使用 PowerShell 中的 CSOM 我们可以从本地计算机连接到 SharePoint Online,我们如何使用 PowerShell 中的 CSOM 连接到本地 SharePoint 2013?

注意:我正在使用 System.Net.NetworkCredential 获取 On-Premsie 的用户名和密码

使用下面的脚本,在执行时遇到错误。

Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

function OnPremises-Test {
    $siteUrl = "https://<>/"

    $clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
    $clientContext.Credentials = New-Object System.Net.NetworkCredential("myusername", "password")

    $web = $clientContext.Web 
    $clientContext.Load($web) 
    $clientContext.ExecuteQuery()

Write-Host " Current web title is '$($web.Title)', $($web.Url)"

}

 OnPremises-Test

以下是错误:

使用“0”参数调用“ExecuteQuery”的异常:“远程服务器返回错误:(403) Forbidden。” 在 C:\Users\v-krirao\Desktop\test.ps1:17 char:2 + $clientContext.ExecuteQuery() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : WebException

【问题讨论】:

    标签: powershell sharepoint sharepoint-2013


    【解决方案1】:

    虽然您的代码在我 2013 年的网站上对我有用 ...Extensions\16\ISAPI... 不特定于 SP2013 尝试以下操作;

    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    

    【讨论】:

      【解决方案2】:

      SharePoint 模式和实践 (PnP) 包含一个 PowerShell 命令库 (PnP PowerShell),允许您对 SharePoint 执行复杂的配置和项目管理操作。这些命令使用 CSOM,并且可以针对 SharePoint OnlineSharePoint On-Premises

      优点是您编写的代码可以简单地替换为一个命令行开关。

      【讨论】:

      • 我遇到了 PnP 问题,因为我必须将脚本移交给其他人进行部署的不同环境,不能要求他们安装 PnP cmdlet。
      • 我知道您必须将其提交到另一个环境。如果可能,只需一个命令即可安装 pnp:“Install-Module SharePointPnPPowerShell2013”​​,您可以将其作为脚本的一部分。开始检查是否安装了 PNP。如果没有,请运行给定的命令。
      • 一开始我不太愿意使用 pnp,但经过一些实验后,我认为这是一个有用且有据可查的框架。它本身并没有回答 OP。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-22
      • 1970-01-01
      • 2016-01-03
      相关资源
      最近更新 更多