Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Cannot get clientContext.Credentials to work (Javascript CSOM)

$
0
0

Am I using the wrong code here? I need to pass the relevant credentials to Sharepoint in order to get list items returned.
I just get a blank page and when I check with developer tools I see the error "NetworkCredential is undefined" if I remove that line I get 401 unauthorised. Below is my code with the paths and credentials blocked out 

<!DOCTYPE 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 = "https://xxx";

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

            //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("https://xxx");
                                            });
                                        });
                    });
                });
            });
        });


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

            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>



Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>