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

查看:14
本文介绍了以编程方式更改 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天全站免登陆