Reorder The Columns In SharePoint List Content Type Programmatically Using CSOM C# And JSOM

Introduction

In this article, you will learn how to reorder the columns in SharePoint List Content Type programmatically, using CSOM C# and JSOM. This is applicable for all SharePoint On Premise platforms.

Prerequisite

The following prerequisite is needed to be followed before reordering the Content Type columns.

  1. Create the site collection in SharePoint.

  2. Navigate to Site and click on “Settings” wheel. Click on “Add an app” or go to “View All Site Content”. Then, click “Add an App”.



  3. Select “Custom List” from the “Your Apps” page.
  4. Give the name of the List in the popup and click Create.



  5. Click on the newly created list.



  6. Go to List Settings.

  7. Click on Advanced Settings.
  8. Give “Allow management of content types to “Yes” and click OK button.
  9. Create one new site column TestColumn by clicking on “Create Column”.

  10. Check the column order by clicking on All Items View.

  11. View the same list in View form by add new item to list.

Reorder Column using CSOM C#

In a SharePoint list, when you enable “Allow management of content types”, you change the order of the fields through the UI. This will affect the order of the fields in forms and when viewing a list item. If you want to do this programmatically, however, Microsoft hasn’t provided a standard way of doing it through the CSOM C# object model, FieldLinkCollection.Reorder() can reorder fields in the list forms. Following are the steps of reordering fields.

  1. Add the References to your C# project. The necessary references are Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll.

  2. Initialize client context object with the site URL.
    1. ClientContext clientContext = new ClientContext("https://SiteURL.com");    
  3. If you are trying to access SharePoint site, then you need to setup the site credentials with credentials parameter and get it set to the client context.
    1. clientContext.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication;   
    2. clientContext.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo("ID67891""Password");   
  4. Get the list using GetByTitle method.
    1. List list = clientContext.web.Lists.GetByTitle("TestList");    
  5. Find the ContentType of the list.

  6. Load and execute the ContentType object.

  7. Get the first content type from the list and find FieldLinks of the ContentType.

  8. Set the order of the fields using internal name.

  9. Reorder columns of FieldLinks using Reorder method.

  10. Update and execute the query. 
Reorder() takes an array of internal names of the fields. Based on the order of this array, Reorder() method reorders the fields in the list forms.
  1. ClientContext clientContext = new ClientContext("https://SiteURL.com");  
  2. clientContext.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication;  
  3. clientContext.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo("ID67891""Password");  
  4. List list = clientContext.web.Lists.GetByTitle("TestList");  
  5. ContentTypeCollection contentTypeColl = list.ContentTypes;  
  6. clientContext.Load(contentTypeColl);  
  7. clientContext.ExecuteQuery();  
  8. var itemContenType = contentTypeColl[0];  
  9. var itemContenTypeFieldLink = itemContenType.FieldLinks;  
  10. string[] filedOrders = {  
  11.     "TestColumn",  
  12.     "Title"  
  13. };  
  14. itemContenTypeFieldLink.Reorder(filedOrders);  
  15. itemContenType.Update(false);  
  16. clientContext.ExecuteQuery();  
View the reordered column of list in view form by adding new item to the list.


Reorder Column using JSOM

  1. Initialize client context object.
    1. var ctx = new SP.ClientContext.get_current();    
  2. Get the list using getByTitle method.
    1. var list = ctx.get_web().get_lists().getByTitle('TestList');    
  3. Find ContentType of the list.

  4. Load and execute the ContentType object and find FieldLinks of the ContentType.
  5. Set the order of the field using internal name.

  6. Reorder columns of FieldLinks using Reorder method.

  7. Update and execute the query.
    1. var listContentTypes;  
    2. var ctx = new SP.ClientContext.get_current();  
    3. var list = ctx.get_web().get_lists().getByTitle('TestList');  
    4. listContentTypes = list.get_contentTypes();  
    5. ctx.load(listContentTypes);  
    6. ctx.executeQueryAsync(GetContentTypes_success, onFailure);  
    7.   
    8. function GetContentTypes_success() {  
    9.     var itemContenType = listContentTypes.getItemAtIndex(0);  
    10.     var itemContenTypeFieldLink = itemContenType.get_fieldLinks();  
    11.     itemContenTypeFieldLink.reorder(['TestColumn''Title']);  
    12.     itemContenType.update(false);  
    13.     ctx.executeQueryAsync(Reorder_success, onFailure);  
    14. }  
    15.   
    16. function onFailure(sender, args) {  
    17.     console.log(args.get_message());  
    18. }  
    19.   
    20. function Reorder_success() {  
    21.     console.log("Completed");  
    22. }  
     
    View the reordered column of list in view form by adding new item to the list.
Summary: Thus, you have learned how to reorder the columns in SharePoint List Content Type programmatically, using CSOM C# and JSOM.

Up Next
    Ebook Download
    View all
    Learn
    View all