使用 Powershell CSOM 的 Sharepoint Online 列表,无法提取列表数据
Sharepoint Online lists with Powershell CSOM, Not able to pull list data
我正在尝试使用 Powershell 连接到 SharePoint Online 和具有完全控制权限的 Client.dll 库,但我无法接收列表数据。我似乎能够连接我的凭据,但无法提取列表数据。
我不断收到以下错误:
MethodInvocationException: \Powershell\script.ps1:36:1
Line |
36 | $Context.ExecuteQuery()
| ~~~~~~~~~~~~~~~~~~~~~~~
| Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (400) Bad Request."
InvalidOperation: \Powershell\script.ps1:44:2
Line |
44 | $ListItems | foreach {
| ~~~~~~~~~~~~~~~~~~~~~~~
| An error occurred while enumerating through a collection: The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be
| explicitly requested..
这是我的代码:
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!"```
请从此处下载并安装 SharePoint Online CSOM 库:
SharePoint Online Client Components SDK
然后像下面这样引用dll:
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\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 中测试,它按预期工作:
我正在尝试使用 Powershell 连接到 SharePoint Online 和具有完全控制权限的 Client.dll 库,但我无法接收列表数据。我似乎能够连接我的凭据,但无法提取列表数据。 我不断收到以下错误:
MethodInvocationException: \Powershell\script.ps1:36:1 Line | 36 | $Context.ExecuteQuery() | ~~~~~~~~~~~~~~~~~~~~~~~ | Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (400) Bad Request."
InvalidOperation: \Powershell\script.ps1:44:2 Line | 44 | $ListItems | foreach { | ~~~~~~~~~~~~~~~~~~~~~~~ | An error occurred while enumerating through a collection: The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be | explicitly requested..
这是我的代码:
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!"```
请从此处下载并安装 SharePoint Online CSOM 库:
SharePoint Online Client Components SDK
然后像下面这样引用dll:
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\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 中测试,它按预期工作: