Reputation: 242
Hi i tried to make the tutorial on MSDN. Here is the link: https://msdn.microsoft.com/en-us/library/office/hh185007%28v=office.14%29.aspx And here is my Java Code:
<script type="text/javascript">
$(document).ready(function () {
var scriptbase = "https://example.at/15/";
$.getScript(scriptbase + "SP.Runtime.js", function () {
$.getScript(scriptbase + "SP.js", doNext());
});
});
var siteUrl = "/knowledge/lzpowerbase";
function doNext() {
console.log("SharePoint geladen!!");
retrieveListItems(siteUrl);
}
function retrieveListItems(siteUrl) {
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('Component Documents');
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>
Every time i try to run it it says: Error: The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
And i have no clue why. I tried to google it but didnt found anything becouse i dont was specific stuff i want everything in the list so i can access and use it. Any Help would be great. Thx
Upvotes: 1
Views: 1511
Reputation: 59338
In this example the specified error occurs due to the line:
$.getScript(scriptbase + "SP.js", doNext());
Due to invalid invocation of doNext
function, collListItem
object is not getting initialized in that case.
Solution
Since jQuery.getScript()
accepts callback function as a second argument, replace the line:
$.getScript(scriptbase + "SP.js", doNext());
with:
$.getScript(scriptbase + "SP.js", doNext);
Upvotes: 1