set column order of fields using CSOM/REST javascript
-
07-10-2020 - |
Pergunta
I am creting list fields using CSOM ecma script. How do I set the column order of the fields to be displayed in the form programmatically? For eg: I have created the following fields programmatically :
Title, Description, Property
I have to change the order in which it has to be displayed in the form programmatically to
Property, Title, Description
How do I set this property?
Solução
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.
Outras dicas
If you want to change field order in view then check below scenario.
I had two different lists and similar view. I wanted to update destination list view field order if user change order in source view.
ViewFieldCollection srcViewFields = srcView.ViewFields;
ViewFieldCollection destViewFields = destView.ViewFields;
var srcArray = srcViewFields.ToArray<string>();
var destArray = destViewFields.ToArray<string>();
foreach (var item in destArray)
{
destViewFields.MoveFieldTo(item, Array.IndexOf(srcArray, item));
destView.Update();
}
Not the answer to the question but in case if you want to modify the status of the column ("Hidden","Required"), the below code is useful (Link)
string[] ContentTypeColumnNames = new string[] { "Column1", "Column2", "Column3" };
using (ClientContext context = new ClientContext("http://yourSiteAddress"))
{
List list = context.Web.Lists.GetByTitle("testdoclib");
context.Load(list);
var x = context.LoadQuery(list.ContentTypes.Where(ct => ct.Name == "Document"));
context.ExecuteQuery();
ContentType ctx = (ContentType)x.FirstOrDefault();
context.Load(ctx);
context.Load(ctx.Fields);
context.Load(ctx.FieldLinks);
context.ExecuteQuery();
foreach (FieldLink f in ctx.FieldLinks)
{
if (ContentTypeColumnNames.Contains(f.Name))
{
f.Hidden = true;
ctx.Update(false);
context.ExecuteQuery();
}
}
context.Load(ctx);
context.ExecuteQuery();
}