是/否SharePoint列表中的类型不会导出到CSV

时间:2018-07-10 02:01:18

标签: powershell sharepoint sharepoint-online

将列表数据传输到CSV时遇到麻烦。列表有四列

  • 标题
  • 年龄
  • 性别
  • SPMember

前三个仅是字符串,但SPMember是是/否类型。导出的CSV在“ SP成员”列上没有任何数据。我尝试使用toString()方法,但显然它返回null。我不确定这是否是“是/否”列中的正常行为,但是任何变通方法/修复会有所帮助。

这是我的代码:

function ExportList($siteUrl, $clientContext, $listN, $txtPath, $exportFile)
{ 

$aQuery = New-Object ([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())

$listItem = $listN.GetItems($aQuery)
$clientContext.Load($listItem)
$clientContext.ExecuteQuery()

$itemCollection = @() 

Write-Host "Creating CSV report" -ForegroundColor Cyan


 foreach ($item in $listItem) {

    $exportItem = New-Object PSObject
    $count = 0

    foreach ($cname in $txtPath){
     Log $txtPath[$count]
     Log $item[$txtPath[$count]].ToString()
     $exportItem | Add-Member -MemberType NoteProperty -Name $txtPath[$count] -Value $item[$txtPath[$count]]
     $count++ 
    }
     $itemCollection += $exportItem
 }
#Export the result Array to CSV file
$itemCollection | Export-CSV $exportFile -NoTypeInformation

Write-host "Done!"  -ForegroundColor Green
} 

$ listN是列表的名称。 $ txtPath是包含标题的txt文件的路径。

1 个答案:

答案 0 :(得分:0)

检查字段内部名称是否为“ SPMember”。

我创建一个测试列表,并添加“是/否”字段“ SPMember”,然后使用下面的PowerShell将列表项导出到csv文件。

#Load SharePoint CSOM Assemblies
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"

##Variables for Processing
$SiteUrl = "https://xxx.sharepoint.com/sites/lz"
$ListName="TestYesNo"
$ExportFile ="D:\ListRpt.csv"
$UserName="lz@xxx.onmicrosoft.com"
$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 = @()

#Fetch each list item value to export to excel
 $ListItems |  foreach {
    $ExportItem = New-Object PSObject
    $ExportItem | Add-Member -MemberType NoteProperty -name "Title" -value $_["Title"]
    if($_["SPMember"] -eq "TRUE"){
        $ExportItem | Add-Member -MemberType NoteProperty -Name "SPMember" -value "Yes"
    }else{
        $ExportItem | Add-Member -MemberType NoteProperty -Name "SPMember" -value "No"
    }    
    #Add the object with above properties to the Array
    $ListItemCollection += $ExportItem
 }
#Export the result Array to CSV file
$ListItemCollection | Export-CSV $ExportFile -NoTypeInformation

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

enter image description here