4

Using the JSOM in SharePoint 2010, if I am creating lists programmatically using something like:

this.myField= oList.get_fields().addFieldAsXml('<Field DisplayName=\'Key\' Type=\'Text\' />', true, SP.AddFieldOptions.defaultValue);   
    var fieldObj = clientContext.castTo(myField,SP.FieldNumber);
    fieldObj.set_defaultValue('-');
    fieldObj.update();
    clientContext.load(myField);

and something list this for updating the list...

this.oList = oWebsite.get_lists().add(listCreationInfo);
oList.set_description('New Announcements List');
oList.update();
clientContext.load(oList);

but what if I want to access like the title column that already gets created and change its default value to "..." and remove it from the default view. Am I able to do that?

EDIT: this seems to be right along the lines of where I am trying to get however I can't seem to get at the ViewFields of the CAML, only the Query portion.

var listCollection = web.get_lists();
var list = listCollection.getByTitle("ExampleList");
var viewCollection = list.get_views();
var view = viewCollection.getByTitle("All Items");

var query = '<OrderBy><FieldRef Name=\'Title\' Ascending=\'False\' /></OrderBy>';
//instead of set_viewQuery I would like to get at the ViewFields...is that possible?
view.set_viewQuery(query);
view.update();

Tried this, but I can't get it to change the list's default All Items View, it seems to only remove it from the forms for me??

   var cxt = new SP.ClientContext.get_current();
        var oWebsite = cxt.get_web();
        this.oList = oWebsite.get_lists().getByTitle('ExampleList);
        fieldCollection = oList.get_fields();
        this.oneField = fieldCollection.getByInternalNameOrTitle("Title");
        this.oneField.set_hidden(true);
        this.oneField.set_required(false);
        this.oneField.update();
        cxt.load(this.fieldCollection);
        cxt.load(this.oneField);
        cxt.executeQueryAsync(Function.createDelegate(this, this.onUpdatedTitleFieldSuccess), Function.createDelegate(this, this.onQueryFailed));

I also noticed that if I use the above, the All Items view still shows Title, but it is removed from the form, and if I browse through the web UI to list settings, the column does not appear in the columns list....weird.

4

How to remove field from List View via CSOM (JavaScript)

function DeleteFieldFromListView(url,listTitle,fieldName,OnSuccess,OnError) {

    var context = new SP.ClientContext(url);
    var list = context.get_web().get_lists().getByTitle(listTitle);
    var view = list.get_defaultView();
    var viewFields = view.get_viewFields();
    context.load(viewFields);

    context.executeQueryAsync(function(sender,args){
        viewFields.remove(fieldName);
        view.update(); 
        context.executeQueryAsync(OnSuccess,OnError);

    },OnError);        
}

Example: remove Title column from default view in List titled Grammy Awards

DeleteFieldFromListView('https://contoso.sharepoint.com/',
'Grammy Awards',
'LinkTitle',
function(){
    console.log('Field has been deleted from View')
},
function(sender,args){
    console.log('Error occured:' + args.get_message());
}
);

LinkTitle corresponds to Title column

  • 1
    awesome! I had to change it slightly from get_defaultView to viewCollection.getByTitle("All Items") in order to get it to work for me. Thanks very much...appreciated! – Justin Jan 13 '14 at 0:55
1

Try the following code:

        fieldCollection = oList.get_fields();
        this.oneField = fieldCollection.getByInternalNameOrTitle("Title");
       this.oneField.set_hidden ();

        this.oneField.update();

        clientContext.load(this.fieldCollection);
        clientContext.load(this.oneField);
        clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess), Function.createDelegate(this, this.OnLoadFailed));

Refer the following for more clarity

http://msdn.microsoft.com/en-us/library/office/jj245336.aspx

http://msdn.microsoft.com/en-us/library/office/jj246815.aspx

  • thanks but I believe that set_hidden() only removes the field from the form display and I am trying to affect the "All Items" default view that gets created. the above does remove it from the forms (which is cool I didn't know about this) but I still can't get it removed from the default view. – Justin Jan 6 '14 at 22:36
  • msdn.microsoft.com/en-us/library/office/jj245336.aspx states that the field is hidden in list views and list forms. – Harshini Hegde Jan 7 '14 at 5:48
  • hmmm...well it must be something I am doing wrong then. will post update to my question to show ya what I got. thanks for the help. – Justin Jan 7 '14 at 10:33

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.