0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SharePoint Client Object Model でビューの表示列を変更するコード

Posted at

ビューの設定画面でやると面倒で間違いやすいですよね。

// 引数は、リスト表示名(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']);
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?