Question

I have a List which is generated and populated with Javascript. However after that I remove a column and add column 'Id' to the default view with the following code:

function fxDeleteFieldFromListView(fieldToRemove, fieldToAdd, OnSuccess, OnError) {

var ctx = new SP.ClientContext(xAppWebUrl);
var oList = ctx.get_web().get_lists().getByTitle(xListTitle);
var oView = oList.get_defaultView();
var viewFields = oView.get_viewFields();
ctx.load(viewFields);

ctx.executeQueryAsync(function (sender, args) {
    viewFields.remove(fieldToRemove);
    viewFields.add(fieldToAdd);
    oView.update();
    ctx.executeQueryAsync(OnSuccess, OnError);

}, OnError);

}

Now, column 'Id' is added AFTER the existing columns in the view, where I would like to have the column at the LEFT side of the view.

How can I reorder the columns with Javascript?

One way I could think of is remove the other 2 columns as well and then add them again after adding the 'Id' column, but I guess there should be some better way.

Thanks in advance!

Was it helpful?

Solution

The ViewFieldCollection has a MoveFieldTo method you can call. The first argument "Id" and the second argument would be an Int32 representing the index (position) where you want the field to appear.

https://msdn.microsoft.com/en-us/library/office/ee659514(v=office.14).aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top