Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

This is the code I wrote to reorder fields in a custom content type:

//get the content type
SPContentType listContentType = myList.ContentTypes[MyContentTypeName];
//get the field name to put first
string firstFieldName = myList.Fields[FirstFieldDisplayName].InternalName;
//get all field names in the content type
List<string> fieldNames = listContentType.FieldLinks
                                .Cast<SPFieldLink>()
                                .Select(a => a.Name).ToList<string>();
//reorder field names
if (fieldNames.Remove(firstFieldName))
{
    fieldNames.Insert(0, firstFieldName);
    listContentType.FieldLinks.Reorder(fieldNames.ToArray<string>());
    listContentType.Update();
}

I stepped through this code using a debugger and I see that the fieldNames list is used correctly - the correct field is removed and then inserted to the start of the list and the Reorder() is executed. Yet the new and edit form shows the fields in the original order. Please help.

share|improve this question
    
@vitule: Just checked some old code of mine and it's essentially the same so not sure why this isn't working... –  Alex Angas Apr 8 '10 at 0:22
    
I tried the code example above and it works for me too. Just out of interest, can you change the order of columns ok using the browser UI? –  Paul Lucas Apr 8 '10 at 17:13
add comment

1 Answer

up vote 2 down vote accepted

So, turns out that the field I'm trying to move was causing some issues when moved to the very first position. It's a custom field derived from SPLookup and I was getting an exception thrown from inside SPLookup when I moved the field to the first position using the UI. Everything was fine when I moved it to the second position, below the "Title" field. I spent an hour trying to figure out what the SPLookup exception meant but didn't figure it out and had to move on.

Anyway, I modified the above code by replacing the line

fieldNames.Insert(0, firstFieldName);

with

fieldNames.Insert(fieldNames.IndexOf("Title") + 1, firstFieldName);

Good enough.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.