Set content type field order programmaticaly

07/11/2019 14:11

Method reorder is used. Following method covers the scenario, when we already have content type, we are adding fields later and need them to put in specific position, because they would be at the end of edit form.

 

internal static void SetFieldPositionInContentType(SPContentType ct, string fieldName, int fieldPosition)
{
var fieldNames = ct.FieldLinks.OfType<SPFieldLink>().Select(f => f.Name).ToList();
fieldNames.RemoveAt(fieldNames.IndexOf(fieldName));
fieldNames.Insert(fieldPosition, fieldName); //first two fields are Title, ContentType
 
ct.FieldLinks.Reorder(fieldNames.ToArray());
ct.Update();
}