1

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.

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.

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.

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.

4

3 回答 3

2

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();
于 2014-06-04T03:12:37.640 回答
2

我查看了 Column ordering page (formEdt.aspx) 的来源,看起来他们使用的是 Web 服务,而不是对象模型:

function DoBuildAndSubmit()
{
    var numFound, currentSelect, selectValue;
    var form = document.forms.aspnetForm;
    var numFields = form["numSelects"].value;
    var xml = "<Fields>";
    numFound = 0;
    while(numFound < numFields)
    {
        for(x = 0; x < numFields; x++)
        {
            currentSelect = form["FormPosition" + x];
            if(currentSelect.selectedIndex == numFound)
            {
                selectValue = currentSelect.options[numFound].value;
                xml = xml + "<Field Name=\"" + selectValue + "\"/>" + "\n";
                numFound++;
            }
        }
    }
    for(x = numFields ; x < 67; x++)
        xml  = xml + "<Field Name=\"" + form["FormPosition" + x].value + "\"/>"  + "\n";
    xml = xml + "</Fields>";
    document.frmLayoutSubmit["ReorderedFields"].value=xml;
    document.frmLayoutSubmit.action = "http://local/_vti_bin/owssvr.dll?CS=65001";
    document.frmLayoutSubmit.submit();
}

现在,也许可以通过对象模型来完成,但是当 UI 正在启动时,我对此没有很好的感觉。

于 2010-10-14T22:18:40.250 回答
0

我使用了您答案中的代码,除了我以编程方式检查了我想要重新排序的列表的内容类型和字段。

//第1步(可选):列出列表的内容类型和字段以查看列表中的内容

 SPList list = web.Lists[strListName];

 string strRet="";
 foreach (SPContentType spct in list.ContentTypes)
                {
                    strRet += "<strong>Content Type: </strong>" + spct.Name + ", <strong>Fields</strong>: <br />";
                    foreach (SPField field in spct.Fields)
                    {

                        if (strFieldInfo != "")
                        {
                            strFieldInfo += ", ";
                        }

                        strFieldInfo += "\"" + field.StaticName + "\"";
                    }
                    strRet += strFieldInfo + "<br />-----<br />";
                }

//Output the results
lblOutput.Text = strRet;

现在,您将了解您的列表有多少内容类型以及列表中有哪些字段。

默认情况下,如果未启用内容类型管理,您将拥有一种包含所有字段的内容类型。

上述代码的示例输出:

内容类型:事件,字段

“ContentType”、“Title”、“Location”、“EventDate”、“EndDate”、“Description”、“fAllDayEvent”、“fRecurrence”、“WorkspaceLink”、“EventType”、“UID”、“RecurrenceID”、“EventCanceled” ”、“持续时间”、“RecurrenceData”、“时区”、“XMLTZone”、“MasterSeriesItemID”、“工作区”、“课程”、“课程位置”

接下来的第 2 步是更改内容类型的顺序。您可以从第 1 步的输出中剪切和粘贴,重新排序,然后添加“{”和“};” 围绕它为您想要的排序创建字符串数组。

 if (list.ContentTypes.Count > 0)
                {

                    SPContentType ct = list.ContentTypes[0]; //Specify the content type here, if you have more than one content type in your list.

                    string[] fieldnames = { "ContentType", "Title", "Course", "CourseLocation",  "EventDate", "EndDate", "Description", "fAllDayEvent", "fRecurrence", "WorkspaceLink", "EventType", "UID", "RecurrenceID", "EventCanceled", "Duration", "RecurrenceData", "TimeZone", "XMLTZone", "MasterSeriesItemID", "Workspace", "Location"};
                    ct.FieldLinks.Reorder(fieldnames);
                    web.AllowUnsafeUpdates = true;
                    ct.Update(true);
                    web.AllowUnsafeUpdates = false;
                }
于 2012-10-22T07:26:09.170 回答