Sharepoint - set column order of fields using CSOM/REST javascript
FieldLinkCollection.Reorder() can reorder fields in list forms. Followings are the steps of reordering fields.
- Find the list
- Find
ContentType
- Find
FieldLinks
of theContentType
- Reorder columns of
FieldLinks
Reorder()
takes an array of Internal Names of the fields. Based on the order of this array, Reorder()
method reorders the fields in list forms.
Example
In my Tender List, I am going to reorder Title
and Tender Number
fields.
Using CSOM C# :
List list = web.Lists.GetByTitle("Tender List");
ContentTypeCollection contentTypeColl = list.ContentTypes;
clientContext.Load(contentTypeColl);
clientContext.ExecuteQuery();
var itemContenType = contentTypeColl[0];
var itemContenTypeFieldLink = itemContenType.FieldLinks;
string[] filedOrder = {
"Tender_x0020_Number",
"Title"
};
itemContenTypeFieldLink.Reorder(filedOrder);
itemContenType.Update(false);
clientContext.ExecuteQuery();
In my previous forms, The order was Title
, Tender Number
. Now it is
Using JSOM
var listContentTypes;
var ctx = new SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle('Tender List');
listContentTypes = list.get_contentTypes();
ctx.load(listContentTypes);
ctx.executeQueryAsync(get_contentTypes_success, onFailure);
function get_contentTypes_success() {
var itemContenType = listContentTypes.getItemAtIndex(0);
var itemContenTypeFieldLink = itemContenType.get_fieldLinks();
itemContenTypeFieldLink.reorder(['Tender_x0020_Number', 'Title']);
itemContenType.update(false);
ctx.executeQueryAsync(field_reorder_success, onFailure);
}
function onFailure(sender, args) {
console.log(args.get_message());
}
function field_reorder_success() {
console.log("done");
}
Chrome Extension
Finally, I wrote a Chrome extension to reorder list/library columns. It is open source. You are always welcome for suggestion and contribution.
https://github.com/dipongkor/reorder-rolumns