以编程方式更改 Sharepoint 2007 列表中的字段顺序

发布于 2024-09-27 16:01:24 字数 668 浏览 2 评论 0原文

我通过一项功能以编程方式将两个新字段添加到现有的 Sharepoint 列表中。字段已成功添加,但我无法调整列顺序。

此任务只需通过 UI 通过转到“列表设置”然后“列排序”即可完成,但我无法以编程方式完成该任务。

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

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();
}

在本例中,列表已经具有“示例一”和“示例三”列,并且我稍后会添加“示例二”,然后尝试订购它们。

然而,这种方法对我来说不起作用,所以如果有人对此有意见,我们将不胜感激。

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

任何意见将不胜感激,感谢您的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若水微香 2024-10-04 16:01:24

我查看了列排序页面的源代码(formEdt.aspx),看起来他们使用网络服务,而不是对象模型:

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();
}

现在,可能可以通过对象模型来完成,但我没有当用户界面下注时,感觉很好。

I took a look at the source of the Column ordering page (formEdt.aspx), it looks like they use web services, not the object model:

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();
}

Now, it might be possible to do through the object model, but I don't have a good feeling about it when the UI is punting.

秋叶绚丽 2024-10-04 16:01:24

这是一个 powershell 版本:

# 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();

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();
简美 2024-10-04 16:01:24

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

//第 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”、“标题”、“位置”、“EventDate”、“EndDate”、“描述” "、"fAllDayEvent"、"fRecurrence"、"WorkspaceLink"、"EventType"、"UID"、"RecurrenceID"、"EventCanceled"、"持续时间"、"RecurrenceData"、"TimeZone"、"XMLTZone"、"MasterSeriesItemID"、 “Workspace”、“Course”、“CourseLocation”

接下来第 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;
                }

I used the code from your answer, except I programmatically examined the content types and fields for the list I wanted to re-order.

//Step 1 (optional): List out the content types and fields for your list to see what is in the list

 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;

Now, you'll have an idea of how many content types your list has and what fields are in the list.

By default, if content type management is not enabled, you'll have one content type that has all the fields.

Sample output from the above code:

Content Type: Event, Fields:

"ContentType", "Title", "Location", "EventDate", "EndDate", "Description", "fAllDayEvent", "fRecurrence", "WorkspaceLink", "EventType", "UID", "RecurrenceID", "EventCanceled", "Duration", "RecurrenceData", "TimeZone", "XMLTZone", "MasterSeriesItemID", "Workspace", "Course", "CourseLocation"

Next Step 2 is to change the order of the content type. You can cut and paste from the output from step 1, re-order it, and add "{" and "};" around it to create the string array for the ordering you want.

 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;
                }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文