CSOM не может пройти мимо SP не определено - PullRequest
0 голосов
/ 06 марта 2019

Я проверил этот сайт и другие и нашел ответы, которые я пробовал, но, должно быть, отсутствует правильный.Сначала займитесь 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>

1 Ответ

0 голосов
/ 06 марта 2019

Вы можете попробовать это.

<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, чтобы его можно было открыть в браузере.enter image description here

...