繁体   English   中英

将选项从 Sharepoint 列表选择字段导出到 csv

[英]Export choices from a Sharepoint list choice field to csv

我是 SharePoint 的新手,但我希望使用 Powershell 将 SharePoint 列表中选择字段中的所有选项导出到 .csv 文件。

希望文件看起来像:

"Choices"
"Choice1"
"Choice2"
"Choice3"

等等。 我需要 .csv 文件中的选项,我将其用于另一个 powershell 脚本中的输入。

我试过谷歌搜索,但每个结果似乎都是关于如何导出特定选择的结果,如下例所示: http : //blog.metrostarsystems.com/2015/06/05/using-powershell-and-the -sharepoint-2013-csom-to-export-list-data/

上下文:我们维护一个可以访问我们测试网站的 IP(和相关信息)列表。 我们维护该列表,然后手动将网站上的 IP 列入白名单。 由于我们目前有 20 个网站,而且数量还在增加,我们希望将其自动化,因此我们只需要维护 SharePoint 列表并“神奇”地将网站上的 IP 列入白名单。 提到的选择字段是网站,因此当我们添加新网站时,我不想手动更新 .txt 或 csv 文件,我只想根据 SharePoint 列表中的可用选项创建它。

如果您在 SharePoint Web 前端服务器上运行脚本,请使用服务器对象模型

如果您不是从 SharePoint 管理外壳运行此程序,您需要先添加 SharePoint 管理单元

add-pssnapin "Microsoft.SharePoint.Powershell" -ErrorAction SilentlyContinue

现在您可以访问 SharePoint 对象模型,您可以获取列表...

$web = get-spweb http://your.web.url
$list = $web.lists["List Title"]

然后拿到场地...

$field = $list.Fields | where-object {$_.title -eq "Field Title"}
# you can also get the field by its internal name like so:
# $field = $list.Fields.GetFieldByInternalName("FieldInternalName")

然后将字段选择导出到 CSV 并处理 SPWeb 对象。

$field.choices | Export-Csv -path "\WhereverYouWant\YourFile.csv" -notype
$web.dispose()

如果您从 SharePoint 环境外部运行脚本,请使用客户端对象模型

这些方法需要一些 C# 代码,因为 Powershell 不能很好地与需要泛型类型的 .NET 方法配合使用。

它的要点是您将编写一个静态 C# 方法,您可以从 Powershell 调用该方法以获取所需的数据。 在这种情况下,以 XML 形式获取字段模式是获取可用字段选择的好方法。

# Replace these paths with actual, locally accessible paths to the Client and Client.Runtime DLLs
$hive = "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14"
$assemblies = @()
$assemblies += $hive+"\ISAPI\Microsoft.SharePoint.Client.dll"
$assemblies += $hive + "\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" 
$assemblies += "System.Core"

# Code for accessing the SharePoint client object model
$cSharp = @"
using System;
using System.Collections.Generic;
using Microsoft.SharePoint.Client;
namespace SPClient
{
    public class CustomClass{
        public static string GetFieldSchema()
        {
            ClientContext clientContext = new ClientContext(`"http://your/SharePoint/url`");
            List list = clientContext.Web.Lists.GetByTitle(`"List Name Here`");
            Field field = list.Fields.GetByInternalNameOrTitle(`"Field Name Here`");
            clientContext.Load(field);
            clientContext.ExecuteQuery();
            return field.SchemaXml;
        }
    }
}
"@

# Here's the magic where you load the above code and reference the assemblies
Add-Type -TypeDefinition $cSharp -ReferencedAssemblies $assemblies    

# Now you can invoke the custom code to get the XML string
[xml]$schema = [SPClient.CustomClass]::GetFieldSchema()

# Parse the XML as normal
$array = @()
$schema.Field.Choices.Choice | %{ $array += new-object psobject -property @{Choice=$_} }
$array | Export-CSV -notype -path "\WhereverYouWant\YourFile.csv"   

检索站点对象然后引用 .choices 属性就可以了。

对于在 2007 环境下运行的客户端,代码如下。

$spsite=[Microsoft.SharePoint.SPSite]("<site url>")
$rootWebSite=$spsite.RootWeb
$rootWebSite.Fields[0].Choices

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM