LoginSignup
0

More than 5 years have passed since last update.

SharePoint Client Object Model で現在のページがリストのビューページかフォームページの場合に処理を行うコード

Posted at

function execHoge4View() {
    alert('This is ListView page!');
}
function execHoge4Form() {
    alert('This is ListForm page!');
}


(function(func4View, func4Form){
    if(_spPageContextInfo.listId === null || _spPageContextInfo.listId === ""){
        // リストコンテキスト外のため処理終了
        return;
    }

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

    // リストコンテキスト内のページ(ビュー、フォーム等)の場合に現在のリストを取得
    var custList = custCtx.get_web().get_lists().getById(_spPageContextInfo.pageListId);

    // リストビューコレクション オブジェクトの取得指示
    var custViews = custList.get_views();

    // リストビューコレクション オブジェクト取得予約
    custCtx.load(custViews);

    // リストフォームコレクション オブジェクトの取得指示
    var custForms = custList.get_forms();

    // リストフォームコレクション オブジェクト取得予約
    custCtx.load(custForms);

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

            // すべてのリストビューの内容を取得
            // console.log(custViews.get_count());
            var listViewEnumerator = custViews.getEnumerator();
            while (listViewEnumerator.moveNext()) {
                // 各ビューを調査
                var oView = listViewEnumerator.get_current();

                // console.log(String.format( "Title:{0}, ServerRelativeUrl:{1}, ID:{2}",
                //  oView.get_title(), oView.get_serverRelativeUrl(), oView.get_id() ));

                if( currentUrlPath == oView.get_serverRelativeUrl().toLowerCase() ) {
                    // 現在のページがリストビューだった場合
                    func4View();
                    break;
                }

            }

            // すべてのフォームの内容を取得
            // console.log(custForms.get_count());
            var listFormEnumerator = custForms.getEnumerator();
            while (listFormEnumerator.moveNext()) {
                // 各ビューを調査
                var oForm = listFormEnumerator.get_current();

                // console.log(String.format( "FormType:{0}, ServerRelativeUrl:{1}, ID:{2}",
                //  oForm.get_formType(), oForm.get_serverRelativeUrl(), oForm.get_id() ));

                if( currentUrlPath == oForm.get_serverRelativeUrl().toLowerCase() ) {
                    // 現在のページがリストフォームだった場合
                    func4Form();
                    break;
                }

            }
        },
        function(sender, args) { // クエリー失敗時コールバック処理
            console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }
    );
})(execHoge4View, execHoge4Form);

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