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

将选项从Sharepoint列表选项字段导出到csv,csv,powershell,sharepoint,export,Csv,Powershell,Sharepoint,Export,我是SharePoint新手,但我希望使用powershell将SharePoint列表中选项字段中的所有选项导出为.csv文件 希望该文件看起来像: "Choices" "Choice1" "Choice2" "Choice3" 等等。我需要.csv文件中的选项,用于另一个powershell脚本中的输入 我尝试过谷歌搜索,但每个结果似乎都是关于如何为特定选择导出结果,如下面的示例所示: 背景: 我们维护一个IP列表(以及相关信息),这些IP可以访问我们的测试网站。我们维护列表,然后手动将

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

希望该文件看起来像:

"Choices"
"Choice1"
"Choice2"
"Choice3"
等等。我需要.csv文件中的选项,用于另一个powershell脚本中的输入

我尝试过谷歌搜索,但每个结果似乎都是关于如何为特定选择导出结果,如下面的示例所示:

背景:
我们维护一个IP列表(以及相关信息),这些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
$spsite=[Microsoft.SharePoint.spsite](“”)
$rootWebSite=$spsite.RootWeb
$rootWebSite.Fields[0]。选项

我可以看到它被称为选择字段值。这里似乎有帮助:我来看看。