[英]Programatically changing field order in Sharepoint 2007 list
我正在通過功能以編程方式在現有的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以具有正確的字段順序,但是我想看看這是否是最佳方法。
任何輸入將不勝感激,謝謝您的幫助。
這是一個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();
我看了一下列排序頁面(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)插入時,我對此感覺不太好。
除了我以編程方式檢查了要重新排序的列表的內容類型和字段外,我使用了答案中的代碼。
//步驟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;
現在,您將了解列表有多少種內容類型以及列表中有哪些字段。
默認情況下,如果未啟用內容類型管理,則您將擁有一個包含所有字段的內容類型。
上面代碼的輸出示例:
內容類型:事件, 字段 :
下一步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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.