Search This Blog

Saturday, November 26, 2011

Change Column Order in New and Edit Form of SharePoint List or Content Type

Sometimes client have a requirement where they need to show some fields in specific order in new and edit form of list item. By default SharePoint show fields in order in which they add that fields in the list or content type. User also can configure the column ordering via List settings page via Column ordering link. But if developer needs to update the same via code then below code will help them to do that. below are given 2 methods which will change the order of the column in list and content type depend on the call of the method. 



private void ReOrderColumn(List<SPField> fields, SPList list)
{
        try
        {
            Dictionary<Int32,String > listColumnReorder = new Dictionary<Int32,String >();
            int iCounter = 0;
            foreach (SPField field in fields)
            {
                listColumnReorder.Add(iCounter,field.InternalName);
                iCounter++;
            }
            String[] sFields = new String[listColumnReorder.Count];

            foreach (Int32 order in listColumnReorder.Keys)
            {
                sFields[order] = listColumnReorder[order];
            }
            ReOrderColumn(sFields, list.ContentTypes[0]);
        }
        catch (Exception ex)
        {
        }
 }

    private void ReOrderColumn(String[] fieldInternalNameArray, SPContentType objContentType)
    {
        try
        {
            SPFieldLinkCollection fldLinks = objContentType.FieldLinks;
            fldLinks.Reorder(fieldInternalNameArray);
            objContentType.Update();
        }
        catch (Exception ex)
        {
        }
    }


3 comments:

  1. awesome post !! i am looking for the same. you saved my time and effort. Thanks a lot Rahul !!!

    ReplyDelete
  2. Thanks for you code. that what I was searching. it works from first time.

    ReplyDelete
  3. its really nice post. here i am updating with powershell script to set column order.

    http://suryapulipati.blogspot.com/2013/07/column-order-for-sharepoint-2010.html

    ReplyDelete