sharepoint - retrieve parameter from executeQueryAsync, javascript - Stack Overflow

I'm trying to retrieve a parameter from a success method that is called within and executeQueryAsy

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
Add a ment  | 

1 Answer 1

Reset to default 3
clientContext.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] + " " );
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745493282a4630081.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信