2

Using a JavaScript, I create new custom lists including some new fields. After list creation, the default view shows all the added fields. But I also want the columns 'Modified', 'Editor' and 'Attachments' to be shown, which are also created during list creation. Maybe I could change the default list view, so that the other fields are visible too. Or I could create a new list view and set this one as default view.

I found some code for removing fields from a view, but nothing about adding them. Here it is described how to create a new list view, but not how to set this as default view.

I have no idea how to implement this in JavaScript. It would be great if you could provide any code or websites (I have searched for some hours now but didn't find anything useful...).

3

There's an easier way:

...
list = listCollection.getByTitle("yourListTitle");

var defaultView = list.get_defaultView();
var defaultViewFields = defaultView.get_viewFields();
defaultViewFields.add("Title");
defaultViewFields.add("Modified");
defaultViewFields.add("NewField");
defaultViewFields.add("Attachments");
defaultViewFields.add("Status");

defaultView.update();
viewContext.executeQueryAsync(...);

Note that the SP.ViewFieldCollection.add method requires an internal field name or a display name.

  • @ Jason Lee: Thank you very much, this is what I was looking for! Great :) – Nur der Vile zählt May 28 '16 at 8:44
0

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

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.