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.
- Create the site collection in SharePoint.
- 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”.
- Select “Custom List” from the “Your Apps” page.
- Give the name of the List in the popup and click Create.
- Click on the newly created list.
- Go to List Settings.
- Click on Advanced Settings.
- Give “Allow management of content types” to “Yes” and click OK button.
- Create one new site column TestColumn by clicking on “Create Column”.
- Check the column order by clicking on All Items View.
- 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.
- Add the References to your C# project. The necessary references are Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll.
- Initialize client context object with the site URL.
- ClientContext clientContext = new ClientContext("https://SiteURL.com");
- 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.
- clientContext.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication;
- clientContext.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo("ID67891", "Password");
- Get the list using GetByTitle method.
- List list = clientContext.web.Lists.GetByTitle("TestList");
- Find the ContentType of the list.
- Load and execute the ContentType object.
- Get the first content type from the list and find FieldLinks of the ContentType.
- Set the order of the fields using internal name.
- Reorder columns of FieldLinks using Reorder method.
- 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.
- ClientContext clientContext = new ClientContext("https://SiteURL.com");
- clientContext.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication;
- clientContext.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo("ID67891", "Password");
- List list = clientContext.web.Lists.GetByTitle("TestList");
- ContentTypeCollection contentTypeColl = list.ContentTypes;
- clientContext.Load(contentTypeColl);
- clientContext.ExecuteQuery();
- var itemContenType = contentTypeColl[0];
- var itemContenTypeFieldLink = itemContenType.FieldLinks;
- string[] filedOrders = {
- "TestColumn",
- "Title"
- };
- itemContenTypeFieldLink.Reorder(filedOrders);
- itemContenType.Update(false);
- clientContext.ExecuteQuery();
View the reordered column of list in view form by adding new item to the list.
Reorder Column using JSOM
- Initialize client context object.
- var ctx = new SP.ClientContext.get_current();
- Get the list using getByTitle method.
- var list = ctx.get_web().get_lists().getByTitle('TestList');
- Find ContentType of the list.
- Load and execute the ContentType object and find FieldLinks of the ContentType.
- Set the order of the field using internal name.
- Reorder columns of FieldLinks using Reorder method.
- Update and execute the query.
- var listContentTypes;
- var ctx = new SP.ClientContext.get_current();
- var list = ctx.get_web().get_lists().getByTitle('TestList');
- listContentTypes = list.get_contentTypes();
- ctx.load(listContentTypes);
- ctx.executeQueryAsync(GetContentTypes_success, onFailure);
-
- function GetContentTypes_success() {
- var itemContenType = listContentTypes.getItemAtIndex(0);
- var itemContenTypeFieldLink = itemContenType.get_fieldLinks();
- itemContenTypeFieldLink.reorder(['TestColumn', 'Title']);
- itemContenType.update(false);
- ctx.executeQueryAsync(Reorder_success, onFailure);
- }
-
- function onFailure(sender, args) {
- console.log(args.get_message());
- }
-
- function Reorder_success() {
- console.log("Completed");
- }
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.