简体   繁体   English

如何在 PowerShell 中使用 CSOM 连接到本地 SharePoint 2013

[英]How to connect to SharePoint 2013 on-premise using CSOM in PowerShell

I am aware that using CSOM in PowerShell we can connect to SharePoint Online from local machine, how can we connect to SharePoint 2013 on-premise using CSOM in PowerShell?我知道在 PowerShell 中使用 CSOM 我们可以从本地计算机连接到 SharePoint Online,我们如何使用 PowerShell 中的 CSOM 连接到本地 SharePoint 2013?

Note: I am using System.Net.NetworkCredential to get username and password for On-Premsie注意:我正在使用 System.Net.NetworkCredential 获取 On-Premsie 的用户名和密码

Used the below script, getting the errors while executing it.使用下面的脚本,在执行它时得到错误。

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

Below are the errors:以下是错误:

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

SharePoint Patterns and Practices (PnP) contains a library of PowerShell commands ( PnP PowerShell ) that allows you to perform complex provisioning and artifact management actions towards SharePoint. SharePoint 模式和实践 (PnP) 包含一个 PowerShell 命令库 ( PnP PowerShell ),允许您对 SharePoint 执行复杂的预配和项目管理操作。 The commands use CSOM and can work against both SharePoint Online and SharePoint On-Premises .这些命令使用 CSOM,并且可以针对SharePoint OnlineSharePoint On-Premises

The advantage is code you have written can be simply replaced with one single commandlet.优点是您编写的代码可以简单地替换为一个命令行开关。

Although your code works for me on my 2013 site ...Extensions\16\ISAPI... is not specific for SP2013 try the following;尽管您的代码在我的 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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM