views:

50

answers:

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.

+1  A: 

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.

Rich Bennema
Wow... never would of guessed that... I'll give it a try and let you know how it goes.
Matt Shaver