以编程方式更改Sharepoint 2007列表中的字段顺序 [英] Programatically changing field order in Sharepoint 2007 list

查看:116
本文介绍了以编程方式更改Sharepoint 2007列表中的字段顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过功能以编程方式在现有的Sharepoint列表中添加两个新字段.字段已成功添加,但是我无法调整列顺序.

I'm adding in two new fields into an already existing Sharepoint list programmatically through a feature. The fields are being added successfully but I have been unable to adjust the column order.

仅通过UI即可完成此任务,方法是依次转到列表设置"和列顺序",但我无法以编程方式完成该任务.

This task is done simply through the UI by going to List Settings and then Column Ordering, but I have been unable to achieve the task programmatically.

通过一些研究,我发现您可以使用表单的SPContentType来更改FieldLinks的顺序(如下所示):

Through some research I've seen that you can use the SPContentType of the form to change the ordering of the FieldLinks (as follows):

SPList list = web.Lists["Example List"];
if (list.ContentTypes.Count > 0) {
    SPContentType ct = list.ContentTypes[0];
    string[] names = {"Example_x0020_One", "Example_x0020_Two", "Example_x0020_Three"};
    ct.FieldLinks.Reorder(names);
    ct.Update();
}

在此示例中,我列表中已经包含示例一"和示例三"列,稍后我将添加示例二",然后尝试对其进行排序.

In this example, I the list would already have "Example One" and "Example Three" columns, and I would add "Example Two" later and then try to order them.

但是这种方法对我不起作用,因此,如果有人对此进行了输入,将不胜感激.

However this approach did not work for me, so if anyone has input on it, that would be appreciated.

我看到的下一项是手动更改列表的SchemaXml以使字段具有正确的顺序,但是我想看看这是否是最好的方法.

The next item I saw is manually changing the SchemaXml of the list to have the proper order of the fields, but I wanted to see if this was the best method.

任何输入将不胜感激,谢谢您的帮助.

Any input would be appreciated, thank you for your help.

推荐答案

以下是Powershell版本:

Here's a powershell version:

# Moves "FieldToBeMoved" after "Description" field
$list = $web.Lists["Documents"]
$ct = $list.ContentTypes[0] # Or find the desired CT

$newOrder = @()
foreach ($field in $ct.Fields)
{
    if ($field.StaticName -ne "FieldToBeMoved")
    {
        $newOrder += $field.StaticName
    }
    if ($field.StaticName -eq "Description")    
    {
        $newOrder += "FieldToBeMoved"
    }
}

$ct.FieldLinks.Reorder($newOrder)
$ct.Update();

这篇关于以编程方式更改Sharepoint 2007列表中的字段顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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