【发布时间】:2023-03-27 18:50:01
【问题描述】:
我正在尝试使用 Powershell 和具有完全控制用户的 Client.dll 库连接到 SharePoint Online,但无法接收列表数据。我似乎能够连接我的凭据,但无法提取列表数据。 我不断收到以下错误:
MethodInvocationException: \Powershell\script.ps1:36:1 线 | 36 | $Context.ExecuteQuery() | ~~~~~~~~~~~~~~~~~~~~~~~ |使用“0”参数调用“ExecuteQuery”的异常:“远程服务器返回错误:(400) 错误请求。”
无效操作:\Powershell\script.ps1:44:2 线 | 44 | $ListItems | foreach { | ~~~~~~~~~~~~~~~~~~~~~~~ |枚举集合时出错:集合尚未初始化。它尚未被请求或请求尚未执行。它可能需要 |明确要求..
这是我的代码:
Add-Type -Path "C:/Program Files/WindowsPowerShell/Modules/SharePointPnPPowerShellOnline/3.19.2003.0/Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:/Program Files/WindowsPowerShell/Modules/SharePointPnPPowerShellOnline/3.19.2003.0/Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:/Program Files/WindowsPowerShell/Modules/SharePointPnPPowerShellOnline/3.19.2003.0/Microsoft.SharePoint.Client.WorkflowServices.dll"
#Config Parameters
$SiteURL= "https://myorg.sharepoint.com/sites/mysite/"
$ListName="my list"
$ExportFile = "networklocation.csv"
$UserName = "myUser"
$Password = "myPassword"
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Context.Credentials = $Credentials
#Get the List
$List = $Context.web.Lists.GetByTitle($ListName)
#Get All List Items
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$ListItems = $List.GetItems($Query)
$Context.Load($ListItems)
$Context.ExecuteQuery()
#Array to Hold List Items
$ListItemCollection = @()
$ListItems | foreach {
$ExportItem = New-Object PSObject
$ExportItem | Add-Member -MemberType NoteProperty -Name "DepartmentName" -value $_["DepartmentName"]
$ExportItem | Add-Member -MemberType NoteProperty -Name "ProjectTitle" -value $_["ProjectTitle"]
$ExportItem | Add-Member -MemberType NoteProperty -Name "ActivityDescription" -value $_["ActivityDescription"]
$ExportItem | Add-Member -MemberType NoteProperty -Name "Account" -value $_["Account"]
$ExportItem | Add-Member -MemberType NoteProperty -Name "Department" -value $_["Department"]
$ExportItem | Add-Member -MemberType NoteProperty -Name "Fund Code" -value $_["Fund Code"]
$ExportItem | Add-Member -MemberType NoteProperty -Name "Fund Source" -value $_["Fund Source"]
$ExportItem | Add-Member -MemberType NoteProperty -Name "ProjectGroup" -value $_["ProjectGroup"]
$ExportItem | Add-Member -MemberType NoteProperty -Name "Fund Code" -value $_["Fund Code"]
$ListItemCollection += $ExportItem
}
#Export the result Array to CSV file
$ListItemCollection | Export-CSV $ExportFile -NoTypeInformation
Write-host "List data Exported to CSV file successfully!"```
【问题讨论】:
-
奇怪,我检查了你的代码,在我的情况下它没有任何错误。我尝试将 SharePointPnPPowerShellOnline 更新为您拥有的版本 (3.19.2003.0),但我无法通过 Update-Module 下载它……也许它有问题。您能否尝试降低 PnPPowerShellOnline 的版本(我有 3.15.1911 - 有一段时间没有更新它:))。您可以使用 -RequiredVersion 参数安装特定版本。喜欢:'Install-Module -Name SharePointPnPPowerShellOnline -RequiredVersion 3.5.1901.0'
标签: powershell sharepoint-online csom