LoginSignup
1
1

More than 5 years have passed since last update.

SharePoint Client Object Model でページ上のWebパーツ オブジェクトを取得するコード

Posted at
// コンテキスト取得
var custCtx = SP.ClientContext.get_current();

// サイトオブジェクト取得指示
var custWeb = custCtx.get_web();

// ページファイル オブジェクトの取得指示(現在のページ)
JSRequest.EnsureSetup(); // リクエストユーティリティ初期化
var custPage = custWeb.getFileByServerRelativeUrl(JSRequest.PathName);
// ページファイル オブジェクトの取得指示(指定のページファイル)
//var custPage = custWeb.getFileByServerRelativeUrl('(サーバー相対URL)');

// ページファイル オブジェクト取得予約
custCtx.load(custPage);

// Webパーツマネージャ オブジェクトの取得指示
var custWpMgr = custPage.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);

// Webパーツマネージャ オブジェクトの取得予約
custCtx.load(custWpMgr);

// Webパーツコレクション オブジェクトの取得指示
var custWebparts = custWpMgr.get_webParts();

// Webパーツコレクション オブジェクトの取得予約
custCtx.load(custWebparts, "Include(WebPart.Title,WebPart.ZoneIndex)");

// サーバーへのクエリー実行(ここまでのコンテキストに含まれる指示予約を送信)
custCtx.executeQueryAsync(
    function(sender, args) { // クエリー実行時コールバック処理

        // ページファイル の内容を取得
        console.log(custPage);

        // Webパーツマネージャ の内容を取得
        console.log(custWpMgr);

        // Webパーツコレクション の内容を取得
        console.log(custWebparts);

        var webPartEnumerator = custWebparts.getEnumerator();
        while (webPartEnumerator.moveNext()) {
            var oWebPart = webPartEnumerator.get_current().get_webPart();

            console.log(String.format(
                "Title:{0}, Zone:{1}",
                oWebPart.get_title(), oWebPart.get_zoneIndex()
            ));
        }

          // 列の設定を更新したら、サーバーに送信が必要です!
          //custCtx.executeQueryAsync(
          //    function(sender, args) { // クエリー実行時コールバック処理
          //        console.log('送信ヨシ!');
          //    },
          //    function(sender, args) { // クエリー失敗時コールバック処理
          //        console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
          //    }
          //);
    },
    function(sender, args) { // クエリー失敗時コールバック処理
        console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
);
1
1
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
1
1