ビューの設定画面でやると面倒で間違いやすいですよね。
// 引数は、リスト表示名(ListName)、ビュー表示名(ViewName)、列内部名の配列(Fields)
function changeViewFields(ListName, ViewName, Fields){
// コンテキストオブジェクト取得
var custCtx = SP.ClientContext.get_current();
// サイトオブジェクト取得指示
var custWeb = custCtx.get_web();
// リスト(ライブラリ) オブジェクトの取得指示
var custList = custCtx.get_web().get_lists().getByTitle(ListName);
// ビューオブジェクトの取得指示
var custView = custList.get_views().getByTitle(ViewName);
custCtx.load(custView);
// ビュー表示列オブジェクトの取得指示
var custViewFields = custView.get_viewFields();
custCtx.load(custViewFields);
// 取得実行
custCtx.executeQueryAsync(
function () { // 成功時コールバック
// ビューの表示列を初期化
custViewFields.removeAll();
// ビューの表示列を追加
for(i = 0; i < Fields.length; i++) {
custViewFields.add(Fields[i]);
}
// ビューの更新指示
custView.update();
// 更新実行
custCtx.executeQueryAsync(
function () { // 成功時コールバック
alert('Change view [ ' + ListName + ' - ' + ViewName + ' ] fields complete.');
}, function (sender, args) { // 失敗時コールバック
alert('Change view [ ' + ListName + ' - ' + ViewName + ' ] fields failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
);
}, function (sender, args) { // 失敗時コールバック
alert('Get view [ ' + ListName + ' - ' + ViewName + ' ] fields failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
);
}
// 呼び出し例
changeViewFields("おしらせ", "すべてのアイテム", ['Title', 'Created', 'Message']);