Programatically changing field order in Sharepoint 2007 list

北城以北 提交于 2019-12-02 10:53:04

问题


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.


回答1:


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.




回答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();



回答3:


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


来源:https://stackoverflow.com/questions/3936707/programatically-changing-field-order-in-sharepoint-2007-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!