将所有列及其数据值导出到列表中 [英] Export all the columns with their data values in a list

查看:157
本文介绍了将所有列及其数据值导出到列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须导出特定SharePoint列表中的所有列以及这些列中包含的数据.

I have to export all the columns in a particular SharePoint list along with the data contained in the columns.

我目前能够获取所有列名,但不能获取数据.需要帮助.下面是我的代码.

I am currently able to get all column names but not data. Need help with that. Below is my code.

$url = "$url"
$listName = "$list"
$path ="c:\ColumnsOfList.csv"

$web = Get-SPWeb $url
$list = $web.Lists.TryGetList($listName)
$list.ContentTypes | % { $_.FieldLinks } | select Name |  Export-Csv -Path $path

推荐答案

有几种不同的实现方法,但要了解的最大问题是,您需要遍历列表中的项目(而不仅仅是通过列表中的字段).

There are a few different ways to do it, but the big thing to understand is that you'll need to iterate through the items in the list (not just through the fields on the list).

$url = "$url"
$listName = "$list"
$path ="c:\ColumnsOfList.csv"

$web = Get-SPWeb $url
$list = $web.Lists.TryGetList($listName)
$fields = $list.ContentTypes | %{ $_.FieldLinks } | select Name
$items = @() #array to store objects representing list items
$list.items | %{ 
    $item = $_; 
    $hash = @{}; #hash table to store field name-value pairs
    $fields | %{ $hash[$_.Name] = $item[$_.Name]  }; 
    $items += new-object psobject -Property $hash }
$items | Export-Csv -Path $path

请注意,此方法尝试获取列表中的所有项目,这可能效率不高,并且如果列表超过列表视图阈值(默认限制为5000个项目),该方法将失败.

Note that this approach tries to get all the items in the list, which is potentially inefficient and will fail if the list surpasses the list view threshold (by default throttled to 5000 items).

要访问列表项的筛选子集,请使用适当的CAML创建一个SPQuery对象以选择所需的项,然后调用$list.GetItems($spquery)而不是直接访问$list.items属性.

To access a filtered subset of list items, create an SPQuery object with appropriate CAML to select the desired items, then call $list.GetItems($spquery) instead of accessing the $list.items property directly.

更新了代码以导出列的显示名称,而不是内部名称

Updated code to export display names of columns instead of internal names

$url = "$url"
$listName = "$list"
$path ="c:\ColumnsOfList.csv"

$web = Get-SPWeb $url
$list = $web.Lists.TryGetList($listName)
$fields = $list.ContentTypes | %{ $_.FieldLinks } | select Name, DisplayName
$items = @() #array to store objects representing list items
$list.items | %{ 
    $item = $_; 
    $hash = @{}; #hash table to store field name-value pairs
    $fields | %{ $hash[$_.DisplayName] = $item[$_.Name]  }; 
    $items += new-object psobject -Property $hash }
$items | Export-Csv -Path $path

这篇关于将所有列及其数据值导出到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆