以编程方式更改 Sharepoint 2007 列表中的字段顺序
我通过一项功能以编程方式将两个新字段添加到现有的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我查看了列排序页面的源代码(formEdt.aspx),看起来他们使用网络服务,而不是对象模型:
现在,可能可以通过对象模型来完成,但我没有当用户界面下注时,感觉很好。
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:
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.
这是一个 powershell 版本:
Here's a powershell version:
我使用了您答案中的代码,但我以编程方式检查了我想要重新排序的列表的内容类型和字段。
//第 1 步(可选):列出列表的内容类型和字段,以查看列表中的内容
现在,您将了解列表有多少种内容类型以及列表中有哪些字段。
默认情况下,如果未启用内容类型管理,您将拥有一种包含所有字段的内容类型。
上述代码的示例输出:
内容类型:事件,字段:
“ContentType”、“标题”、“位置”、“EventDate”、“EndDate”、“描述” "、"fAllDayEvent"、"fRecurrence"、"WorkspaceLink"、"EventType"、"UID"、"RecurrenceID"、"EventCanceled"、"持续时间"、"RecurrenceData"、"TimeZone"、"XMLTZone"、"MasterSeriesItemID"、 “Workspace”、“Course”、“CourseLocation”
接下来第 2 步是更改内容类型的顺序。您可以剪切并粘贴步骤 1 的输出,重新排序,然后添加“{”和“};”围绕它来创建您想要的排序的字符串数组。
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
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.