我正在尝试从成功方法中检索参数,并使用Javascript ECMA脚本执行executeQueryAsync。
我在这里尝试了建议
http://www.learningsharepoint.com/2013/08/07/passing-parameters-to-success-method-in-executequeryasync-sharepoint/
但是我在MicrosfotAjax.js文件中收到以下错误:
“ Collection尚未初始化。尚未请求或尚未执行请求。可能需要显式请求。”在警报中(_returnParam);命令时,我得到一个未定义的值,相反它应该说“ hello”。在获得成功函数以返回listItemInfo数组之前,我想演示一个简单的变量返回。
谢谢您的考虑
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());
}
最佳答案
clientContext.executeQueryAsync(Function.createDelegate(this, function(){_returnParam = onQuerySucceeded();}), Function.createDelegate(this, this.onQueryFailed));
alert(_returnParam);
这是正常的。
executeQueryAsync
是异步的,因此会调用executeQueryAsync
(=完成AJAX请求),然后立即在不等待AJAX完成的情况下,调用_returnParam仍然为空的alert(_returnParam)
。如果要执行其他操作,则需要在
onQuerySucceeded
的末尾调用另一个函数。顺便说一句,我一直认为Microsoft的方法确实很难使用。如果您有兴趣,我已经创建了一个用于处理Sharepoint的库(称为SharepointPlus)。对于您要实现的目标,代码将是这样的(我也使用数组检查了您的代码):
$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] + " " );
})