【问题标题】:Sharepoint Online lists with Powershell CSOM, Not able to pull list data带有 Powershell CSOM 的 Sharepoint Online 列表,无法提取列表数据
【发布时间】: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


【解决方案1】:

请从此处下载并安装 SharePoint Online CSOM 库:

SharePoint Online Client Components SDK

然后像下面这样引用dll:

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"


#Config Parameters
$SiteURL= "https://Tenant.sharepoint.com/sites/dev/"
$ListName="mylist7"
$ExportFile = "D:\UserData1.csv"
$UserName = "user@Tenant.onmicrosoft.com"
$Password = "password"

#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 "Title" -value $_["Title"]
    $ListItemCollection += $ExportItem
 }
#Export the result Array to CSV file
$ListItemCollection | Export-CSV $ExportFile -NoTypeInformation

Write-host "List data Exported to CSV file successfully!"```

在 PowerShell ISE 中测试,它按预期工作:

【讨论】:

  • 这些客户端组件会保持更新吗? NuGet ones 似乎是最新的。可以改用 NuGet 包吗?喜欢从 lib/net45 文件夹中提取文件?
  • @Abhitalks 是的,你也可以使用 Nuget 的 csom 库,只需在上面的 PowerShell 代码中更改引用路径。
猜你喜欢
  • 2016-09-06
  • 2023-03-19
  • 2014-03-13
  • 2018-01-03
  • 2017-01-30
  • 2017-12-01
  • 2019-08-03
  • 2017-12-31
  • 1970-01-01
相关资源
最近更新 更多