STORM
STORM

Reputation: 4331

SCRIPT5022: The collection has not been initialized. It has not been requested or the request has not been executed

i have a simple javascript which throws me the following error:

SCRIPT5022: 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.

I am using it under O365 inside an Content Editor WebPart. I have a list called "myTestList" with several items and one column called "points". I want to retrieve only the items which i have created and sum the points to show it inside a div. This is my code.

<div id="myPoints" style="font-size: 50px;">0​</div> 

<script language="javascript" type="text/javascript">

SP.SOD.executeOrDelayUntilScriptLoaded(retrieveMyItems, "sp.js");

function retrieveMyItems() {
    var clientContext = new SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('myTestList');

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Author' /><Value Type='Integer'><UserID /></Value></Eq></Where></Query></View>");
    this.collListItem = oList.getItems(camlQuery);

    clientContext.load(collListItem, 'Include(points)');

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onMyItemsQuerySucceeded), Function.createDelegate(this, this.onMyItemsQueryFailed));
}

function onMyItemsQuerySucceeded(sender, args) {
    var listItemInfo = 0;
    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        listItemInfo = listItemInfo + parseInt(oListItem.get_item('points'));
    }

    var div = document.getElementById("myPoints");
    div.innerHTML = listItemInfo;
}

function onMyItemsQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

</script>​

Whats wrong here?

Update Solved! The set_viewXml

camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Author' /><Value Type='Integer'><UserID Type='Integer'/></Value></Eq></Query></Where></View>");

I had to add .

Upvotes: 0

Views: 2931

Answers (1)

STORM
STORM

Reputation: 4331

Update Solved! The set_viewXml

camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Author' /><Value Type='Integer'><UserID Type='Integer'/></Value></Eq></Query></Where></View>");

had to be extend with

<Query><Where>

Upvotes: 1

Related Questions