19

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?

19

FieldLinkCollection.Reorder() can reorder fields in list forms. Followings are the steps of reordering fields.

  1. Find the list
  2. Find ContentType
  3. Find FieldLinks of the ContentType
  4. 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.

enter image description here

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

enter image description here

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

  • How would you do this for simpler lists that do not have content types enabled? I just want to reorder columns on the list itself. – Dinerdo Jul 17 '17 at 17:19
0

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();
}
0

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();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.