I have tried using this caml query, but it does not work. I guess it is the caml itself. There is no effect in the list view called "All Items".
var viewContext = SP.ClientContext.get_current();
var web = viewContext.get_web();
var listCollection = web.get_lists();
list = listCollection.getByTitle("yourListTitle");
viewCollection = list.get_views();
view = viewCollection.getByTitle("All Items");
var camlQuery = new SP.CamlQuery();
var query = "<View><ViewFields><FieldRef Name="Title" /><FieldRef Name="Modified" /></ViewFields></View>";
//also tried this: var query = "<Where><Eq><FieldRef Name=\"Modified\" /><Value Type='text'>my text</Value> </Eq></Where>";
camlQuery.set_viewXml(query);
view.set_viewQuery(camlQuery);
view.update();
viewContext.load(view);
viewContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededView), Function.createDelegate(this, this.onQueryFailed));
Is it still the right approach or am I completely on the wrong track?
EDIT: So, what does work, is deleting the old (default) view and creating a new one. Then there is still only one view, but I can define the fields to be shown.
Delete the existing view:
viewContext = SP.ClientContext.get_current();
var web = viewContext.get_web();
var listCollection = web.get_lists();
list = listCollection.getByTitle("yourListTitle");
viewCollection = list.get_views();
view = viewCollection.getByTitle("All Items");
view.deleteObject();
viewContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQuery));
Create new view:
var viewContext = SP.ClientContext.get_current();
var web = viewContext.get_web();
var listCollection = web.get_lists();
list = listCollection.getByTitle("yourListTitle");
viewCollection = list.get_views();
viewContext.load(viewCollection);
var createView = new SP.ViewCreationInformation();
createView.set_title("All Items");
var viewFields = ["Title","Modified","NewField","Attachments","Status"];
createView.set_viewFields(viewFields);
viewCollection.add(createView);
viewContext.load(viewCollection);
viewContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQuery));
This code works for me. Still, it would be nicer just to modify the existing view. But the users don't see any difference, so that's alright.
EDIT 2: The code below changes the fields in a view. This is my final solution.
var context = SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle("yourListTitle");
var view = list.get_views().getByTitle("All Items");
var fieldNames = view.get_viewFields();
context.load(view);
context.load(fieldNames);
var defaultView = list.get_defaultView();
context.executeQueryAsync(
function(){
var viewNewFields = ["Title","Status","NewField","Modified","Editor","Created","Attachments"];
// delete old fields
var fieldNamesCount = fieldNames.get_count();
for(var i = 0; i < fieldNamesCount;i++) {
var fName = fieldNames.getItemAtIndex(0);
fieldNames.remove(fName);
}
// add new fields
for(var i = 0; i < viewNewFields.length;i++) {
fieldNames.add(viewNewFields[i]);
}
view.update();
context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededView), Function.createDelegate(this, this.onQueryFailedView));
},
onQueryFailedView
);