There’s no function in the SharePoint API to reorder the fields of a SharePoint list. You have to make use of the ProcessBatchDate from the SPWeb class.
////// This function reorders the fields in the specified list /// programmatically as specified by the firstFields parameter. /// The fields not included in the firstFields list will be /// added to the list. /// /// The SPList object to update /// A generic list of SPField containing the /// first fields in the order to be displayed. public void ReorderField(SPList list, List firstFields) { List fields = new List(); for (int i=0; i<firstFields.Count; i++) { fields.Add(firstFields[i]); } foreach (SPField field in list.Fields) { if (!fields.Contains(field)) { fields.Add(field); } } StringBuilder sb = new StringBuilder(); XmlTextWriter xmlWriter = new XmlTextWriter(new StringWriter(sb)); xmlWriter.Formatting = Formatting.Indented; xmlWriter.WriteStartElement("Fields"); for (int i=0; i< fields.Count; i++) { xmlWriter.WriteStartElement("Field"); xmlWriter.WriteAttributeString("Name", fields[i].InternalName); xmlWriter.WriteEndElement(); } xmlWriter.WriteEndElement(); xmlWriter.Flush(); using (SPWeb web = list.ParentWeb) { ReorderFields(web, list, sb.ToString()); } } ////// This function reorders the fields in the specified list /// programmatically as specified by the xmlFieldsOrdered parameter /// /// The SPWeb object containing the list /// This function reorders the fields in the /// specified list /// A string in XML-format specifying the /// field order by the location within a xml-tree private void ReorderFields(SPWeb web, SPList list, string xmlFieldsOrdered) { try { string fpRPCMethod = @" {0} REORDERFIELDS {1} {2} "; // relookup list version in order to be able to update it list = web.Lists[list.ID]; int currentVersion = list.Version; string version = currentVersion.ToString(); string RpcCall = string.Format(fpRPCMethod, list.ID, SPHttpUtility.HtmlEncode(xmlFieldsOrdered), version); web.AllowUnsafeUpdates = true; web.ProcessBatchData(RpcCall); } catch (System.Net.WebException err) { // TODO: Log this exception according your exception // handling policies. Console.WriteLine("WARNING:" + err.Message); } }
