2


How to set order of content type fields in Create and Edit form via C# & Sharepoint 2010 Client Object Model or Sharepoint 2010 Web Services?
Thank you.

Nebiross
  • 21
  • 3

1 Answers1

2

The following example demonstrates how to reorder fields in content type using CSOM

var ctId = "0x01020047F5B07616CECD46AADA9B5B65CAFB75";  //Event ct
var listTitle = "Calendar";
using (var ctx = new ClientContext(webUri))
{

     var list = ctx.Web.Lists.GetByTitle(listTitle);
     var contentType = list.ContentTypes.GetById(ctId);


     //retrieve fields
     ctx.Load(contentType,ct => ct.FieldLinks);
     ctx.ExecuteQuery();
     var fieldNames = contentType.FieldLinks.ToList().Select(ct => ct.Name).ToArray();

     fieldNames.ShiftElement(1, 2);   //Ex.:Render Title after Location in Calendar

     //reorder
     contentType.FieldLinks.Reorder(fieldNames);
     contentType.Update(false);
     ctx.ExecuteQuery();
} 

where

public static class ArrayExtensions
{
    //from http://stackoverflow.com/a/7242944/1375553
    public static void ShiftElement<T>(this T[] array, int oldIndex, int newIndex)
    {
        // TODO: Argument validation
        if (oldIndex == newIndex)
        {
            return; // No-op
        }
        T tmp = array[oldIndex];
        if (newIndex < oldIndex)
        {
            // Need to move part of the array "up" to make room
            Array.Copy(array, newIndex, array, newIndex + 1, oldIndex - newIndex);
        }
        else
        {
            // Need to move part of the array "down" to fill the gap
            Array.Copy(array, oldIndex + 1, array, oldIndex, newIndex - oldIndex);
        }
        array[newIndex] = tmp;
    }
}

Result

Before

enter image description here

After

enter image description here

Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
  • 1
    Vadim, thanks for reply! I have already used this way (FieldLinkCollection.Reorder(string[])), but had SharePoint.Client.ServerException like Method "Reorder" doesn't exist. Does this method exist in Sharepoint 2010 API or only in Sharepoint 2013 API? – Nebiross Apr 28 '15 at 09:18