I'm trying to retrieve a parameter from a success method that is called within and executeQueryAsync using Javascript ECMA scripting.
I get tried follwing the suggestion here
/
but I get the following error in the MicrosfotAjax.js file:
"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." in the alert(_returnParam); mand, I get an undefined value when instead it should say "hello". I want to demonstrate a simple variable return before I get the success function to return listItemInfo array.
Thank you for your consideration
function Tblsrch(camlstr){
var siteUrl = '/sites/SIandT%20Project%20Intelligence';
var hello;
var clientContext = new SP.ClientContext(siteUrl);
var oWebsite = clientContext.get_web();
var oList = oWebsite.get_lists().getByTitle('ISATestdata');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(camlstr);
this.collListItem = oList.getItems(camlQuery);
var _returnParam;
clientContext.executeQueryAsync(Function.createDelegate(this, function(){_returnParam = onQuerySucceeded();}), Function.createDelegate(this, this.onQueryFailed));
alert(_returnParam);
}
function onQuerySucceeded(sender, args){
var listItemInfo = new Array();
var rowInd = 0;
var hello;
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
// listItemInfo += '\nType:' + oListItem.get_item('Name2') + ' | ' + oListItem.get_item('Plan') + ' | ' + oListItem.get_item('Type1');
listItemInfo[rowInd] = new Array(10);
listItemInfo[rowInd][0] =oListItem.get_item('Name2');
listItemInfo[rowInd][1] =oListItem.get_item('Type1');
listItemInfo[rowInd][2] = oListItem.get_item('Plan');
listItemInfo[rowInd][3] = oListItem.get_item('Analyse');
listItemInfo[rowInd][4] = oListItem.get_item('Design');
listItemInfo[rowInd][5] = oListItem.get_item('Build');
listItemInfo[rowInd][6] = oListItem.get_item('Test');
listItemInfo[rowInd][7] = oListItem.get_item('Run');
listItemInfo[rowInd][8] = oListItem.get_item('SupportMaintenance');
listItemInfo[rowInd][9] = oListItem.get_item('Link1');
listItemInfo[rowInd][10] = oListItem.get_item('Link2');
rowInd++;
}
alert(listItemInfo[0][0] + " " + listItemInfo[0][1] + " " +listItemInfo[0][2] + " " + listItemInfo[0][3] + " " +listItemInfo[0][4] + " " );
var _returnParam = "hello";
return _returnParam;
}
function onQueryFailed(sender, args){
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
I'm trying to retrieve a parameter from a success method that is called within and executeQueryAsync using Javascript ECMA scripting.
I get tried follwing the suggestion here
http://www.learningsharepoint./2013/08/07/passing-parameters-to-success-method-in-executequeryasync-sharepoint/
but I get the following error in the MicrosfotAjax.js file:
"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." in the alert(_returnParam); mand, I get an undefined value when instead it should say "hello". I want to demonstrate a simple variable return before I get the success function to return listItemInfo array.
Thank you for your consideration
function Tblsrch(camlstr){
var siteUrl = '/sites/SIandT%20Project%20Intelligence';
var hello;
var clientContext = new SP.ClientContext(siteUrl);
var oWebsite = clientContext.get_web();
var oList = oWebsite.get_lists().getByTitle('ISATestdata');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(camlstr);
this.collListItem = oList.getItems(camlQuery);
var _returnParam;
clientContext.executeQueryAsync(Function.createDelegate(this, function(){_returnParam = onQuerySucceeded();}), Function.createDelegate(this, this.onQueryFailed));
alert(_returnParam);
}
function onQuerySucceeded(sender, args){
var listItemInfo = new Array();
var rowInd = 0;
var hello;
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
// listItemInfo += '\nType:' + oListItem.get_item('Name2') + ' | ' + oListItem.get_item('Plan') + ' | ' + oListItem.get_item('Type1');
listItemInfo[rowInd] = new Array(10);
listItemInfo[rowInd][0] =oListItem.get_item('Name2');
listItemInfo[rowInd][1] =oListItem.get_item('Type1');
listItemInfo[rowInd][2] = oListItem.get_item('Plan');
listItemInfo[rowInd][3] = oListItem.get_item('Analyse');
listItemInfo[rowInd][4] = oListItem.get_item('Design');
listItemInfo[rowInd][5] = oListItem.get_item('Build');
listItemInfo[rowInd][6] = oListItem.get_item('Test');
listItemInfo[rowInd][7] = oListItem.get_item('Run');
listItemInfo[rowInd][8] = oListItem.get_item('SupportMaintenance');
listItemInfo[rowInd][9] = oListItem.get_item('Link1');
listItemInfo[rowInd][10] = oListItem.get_item('Link2');
rowInd++;
}
alert(listItemInfo[0][0] + " " + listItemInfo[0][1] + " " +listItemInfo[0][2] + " " + listItemInfo[0][3] + " " +listItemInfo[0][4] + " " );
var _returnParam = "hello";
return _returnParam;
}
function onQueryFailed(sender, args){
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
Share
Improve this question
edited Jan 26, 2015 at 1:45
Bergi
667k161 gold badges1k silver badges1.5k bronze badges
asked Sep 21, 2013 at 19:49
mogolimogoli
2,3958 gold badges29 silver badges42 bronze badges
1 Answer
Reset to default 3clientContext.executeQueryAsync(Function.createDelegate(this, function(){_returnParam = onQuerySucceeded();}), Function.createDelegate(this, this.onQueryFailed));
alert(_returnParam);
It's normal. executeQueryAsync
is asynchrone, so executeQueryAsync
is called (= an AJAX request is done), and immediately after, without waiting for the AJAX to be pleted, you call alert(_returnParam)
with _returnParam that is still void.
You need to call another function at the end of onQuerySucceeded
if you want another action to be done.
By the way, I always thought that the Microsoft way is really difficult to use. I've created a library to deal with Sharepoint, if you are interested (it's called SharepointPlus). For what you want to achieve, the code would be something like that (I've also review your code with the array):
$SP().list("ISATestdata", "/sites/SIandT%20Project%20Intelligence").get({fields:"Name2,Type1,Plan,Analyse,Design,Build,Test,Run,SupportMaintenance,Link1,Link2",where:"Name2 = 'Something'"}, function(data) {
var listItemInfo = [];
for (var i=0, len=data.length; i<len; i++) {
listItemInfo[i] = [];
listItemInfo[i].push(data[i].getAttribute("Name2"));
listItemInfo[i].push(data[i].getAttribute("Type1"));
listItemInfo[i].push(data[i].getAttribute("Plan"));
listItemInfo[i].push(data[i].getAttribute("Analyse"));
listItemInfo[i].push(data[i].getAttribute("Design"));
listItemInfo[i].push(data[i].getAttribute("Build"));
listItemInfo[i].push(data[i].getAttribute("Test"));
listItemInfo[i].push(data[i].getAttribute("Run"));
listItemInfo[i].push(data[i].getAttribute("SupportMaintenance"));
listItemInfo[i].push(data[i].getAttribute("Link1"));
listItemInfo[i].push(data[i].getAttribute("Link2"));
}
alert(listItemInfo[0][0] + " " + listItemInfo[0][1] + " " +listItemInfo[0][2] + " " + listItemInfo[0][3] + " " +listItemInfo[0][4] + " " );
})