CSOM 无法通过 SP 未定义

CSOM can't get past SP is undefined

我检查了这个网站和其他网站并找到了我试过但一定是遗漏了正确答案的答案。首先尝试使用 CSOM,因为我通常会在 C# 服务器端执行此类操作。正如您在 getScript 函数中看到的那样,我已经提取了我能想到的所有内容,但仍然没有用。文件声称已准备就绪,我通过写信给 console.log 确认了这一点。请问我错过了什么。错误出现在函数 retrieveListItems() 的第二行。

     <html>
<head>
<title></title>

<script src="scripts/jquery-1.7.2.min.js" type="text/javascript">
</script>
<script type="text/javascript" src="scripts/MicrosoftAjax.js">
</script>
</head>
<body>

<script type="text/javascript">
    var hostweburl;
    // Load the required SharePoint libraries.

        // Get the URI decoded URLs.
        hostweburl = "https://hubble.whanganui.govt.nz";

        var scriptbase = hostweburl + "/_layouts/15/";

        // Load the js files and continue to
        // the execOperation function.
        $.getScript(scriptbase + "init.js");
        $.getScript(scriptbase + "MicrosoftAjax.js");
        $.getScript(scriptbase + "sp.core.js");
        $.getScript(scriptbase + "sp.runtime.js");
        $.getScript(scriptbase + "sp.js");
       $(document).ready(function(){
        //ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");
        console.log("ready to roll");
        retrieveListItems("https://path_to_my_SP_site")
       });


        // Continue your program flow here. 
   function retrieveListItems(siteUrl){
    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('Announcements');

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml(
        '<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' + 
        '<Value Type=\'Number\'>1</Value></Geq></Where></Query>' + 
        '<RowLimit>10</RowLimit></View>'
    );
    this.collListItem = oList.getItems(camlQuery);

    clientContext.load(collListItem);
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded), 
        Function.createDelegate(this, this.onQueryFailed)
    ); 
}

function onQuerySucceeded(sender, args) {
    var listItemInfo = '';
    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        listItemInfo += '\nID: ' + oListItem.get_id() + 
            '\nTitle: ' + oListItem.get_item('Title') + 
            '\nBody: ' + oListItem.get_item('Body');
    }

    alert(listItemInfo.toString());
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + 
        '\n' + args.get_stackTrace());
}
        </script>
    </body>
</html>

你可以试试这个。

<html>
<head>
    <title></title>

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
</head>
<body>

    <script type="text/javascript">
        var hostweburl;
        // Load the required SharePoint libraries.

        // Get the URI decoded URLs.
        hostweburl = "http://sp:12001";

        var scriptbase = hostweburl + "/_layouts/15/";

        // Load the js files and continue to
        // the execOperation function.
        //$.getScript(scriptbase + "init.js");
        //$.getScript(scriptbase + "MicrosoftAjax.js");
        //$.getScript(scriptbase + "sp.core.js");
        //$.getScript(scriptbase + "sp.runtime.js");
        //$.getScript(scriptbase + "sp.js");
        $(document).ready(function () {
            //ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");
            $.getScript(scriptbase + 'init.js', function () {
                $.getScript(scriptbase + 'MicrosoftAjax.js', function () {
                    $.getScript(scriptbase + 'sp.core.js', function () {
                        $.getScript(scriptbase + 'SP.Runtime.js',
                                        function () {
                                            $.getScript(scriptbase + 'SP.js', function () {
                                                retrieveListItems("http://sp:12001")
                                            })
                                        });
                    });
                });
            });
        });


        // Continue your program flow here. 
        function retrieveListItems(siteUrl) {
            var clientContext = new SP.ClientContext(siteUrl);
            var oList = clientContext.get_web().get_lists().getByTitle('MyList2');

            var camlQuery = new SP.CamlQuery();
            camlQuery.set_viewXml(
                '<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' +
                '<Value Type=\'Number\'>1</Value></Geq></Where></Query>' +
                '<RowLimit>10</RowLimit></View>'
            );
            this.collListItem = oList.getItems(camlQuery);

            clientContext.load(collListItem);
            clientContext.executeQueryAsync(
                Function.createDelegate(this, this.onQuerySucceeded),
                Function.createDelegate(this, this.onQueryFailed)
            );
        }

        function onQuerySucceeded(sender, args) {
            var listItemInfo = '';
            var listItemEnumerator = collListItem.getEnumerator();

            while (listItemEnumerator.moveNext()) {
                var oListItem = listItemEnumerator.get_current();
                listItemInfo += '\nID: ' + oListItem.get_id() +
                    '\nTitle: ' + oListItem.get_item('Title');
                    //'\nBody: ' + oListItem.get_item('Body');
            }

            alert(listItemInfo.toString());
        }

        function onQueryFailed(sender, args) {
            alert('Request failed. ' + args.get_message() +
                '\n' + args.get_stackTrace());
        }
    </script>
</body>
</html>

这是一个 html 文件,我只是将其重命名为 .aspx 以便可以在浏览器中打开。