| Subcribe via RSS

Reorder SharePoint list fields from code

Juli 19th, 2008 Posted in SharePoint

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.

/// <summary>
/// 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.
/// </summary>
/// <param name="list">The SPList object to update</param>
/// <param name="firstFields">A generic list of SPField containing the
/// first fields in the order to be displayed.</param>
public void ReorderField(SPList list, List firstFields)
{
  List<SPField> fields = new List<SPField>();
 
  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&lt; 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());
  }
}
 
/// <summary>  
/// This function reorders the fields in the specified list 
/// programmatically as specified by the xmlFieldsOrdered parameter  
/// </summary>  
/// <param name="web">The SPWeb object containing the list</param>
/// <param name="list">This function reorders the fields in the 
/// specified list</param>
/// <param name="xmlFieldsOrdered">A string in XML-format specifying the 
/// field order by the location within a xml-tree</param> 
private void ReorderFields(SPWeb web, SPList list,
  string xmlFieldsOrdered)
{
  try
  {
    string fpRPCMethod = @"<?xml version=""1.0"" encoding=""UTF-8""?>  
	<Method ID=""0,REORDERFIELDS"">  
	<SetList Scope=""Request"">{0}</SetList>  
	<SetVar Name=""Cmd"">REORDERFIELDS</SetVar>  
	<SetVar Name=""ReorderedFields"">{1}</SetVar>  
	<SetVar Name=""owshiddenversion"">{2}</SetVar>  
	</Method>";
 
      // 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);
    }
}

5 Responses to “Reorder SharePoint list fields from code”

  1. Rainer Says:

    Thanks man, you saved my day. It’s a shame that we have to use such hacks to get common tasks done.


  2. Sanjay Hari Says:

    Hello alex,
    I was trying to re-order columns for a list but this is the message I repeatedly kept getting “Fields have been added or removed since you began this editing session. Please refresh your view and try again.”.
    Could you throw some light on as to what are the possible reasons for this sorta output.
    Thanks in advance.

    Sanjay K Hari.


  3. Manuel Says:

    Thanks for figuring this out. I knew you had to reproduce the behaviour in formEdt.aspx but super glad you racked your brain for me :) )


  4. Islème Says:

    Good Job !


  5. Ignacio Says:

    Well done! This works really well. Thank you for doing the hard work.


Leave a Reply