Monday, April 28, 2014

How to add code behind to SandBox solution



I have a strange issue with clientcontext
The code fails on a row spListobjItems.get_count(); in onSuccess2()
with this 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.

Here is my code:


function onSuccess2() {
var liCount = spListobjItems.get_count();
for (x = 0; x < liCount; x++) {
var listItem = spListobjItems.itemAt(x);
var videoTitle = listItem.get_item("Title");
}
}

function onFail(sender, args) {
}


var clientContext;
var spWebobj;
var spListobj;
var spCamlQuery;
var spViewXML;
var spListobjItems;
var imgSrc;
var listTitle;

function myCallback() {

$("img").each(function () {
imgSrc = $(this).attr('src');
listTitle = imgSrc.split('/')[1];
try {
clientContext = SP.ClientContext.get_current();
spWebobj = clientContext.get_web();
spListobj = spWebobj.get_lists().getByTitle(listTitle);

spCamlQuery = new SP.CamlQuery();
spViewXML = '<View><Query><Where><Eq><FieldRef Name="FileRef"/><Value Type="Url">' + imgSrc + '</Value></Eq></Where></Query></View>';
spCamlQuery.set_viewXml(spViewXML);
spListobjItems = spListobj.getItems(spCamlQuery);
clientContext.load(spWebobj);
clientContext.load(spListobjItems);

clientContext.executeQueryAsync(onSuccess2, onFail);

}
catch (exception) {
return true;
}
});
return true;
}

$(document).ready(function () {
ExecuteOrDelayUntilScriptLoaded(myCallback, "SP.js");

});





No comments:

Post a Comment