在SharePoint列表视图中,我想复制所选的列表项。当只选择一个项目时,我的代码可以工作,但当选择多个项目时失败。调试代码时,我看到它在调用成功回调之前先遍历所有选定的项。同样,在成功回调中,currItem并不总是充满项数据。
如何逐一获取和处理所选项目?
function copySelected(){
if($("#copyAllButton").hasClass('ms-cui-disabled')){
return;
}
var cc = null;
var web = null;
copyCounter = 0;
failedCounter = 0;
cc = new SP.ClientContext.get_current();
web = cc.get_web();
var currItem = null;
notifyId = SP.UI.Notify.addNotification(duplicatingItemsNotification, true);
var selectedItems;
currList = web.get_lists().getById(SP.ListOperation.Selection.getSelectedList());
selectedItems = SP.ListOperation.Selection.getSelectedItems();
if( selectedItems.length > 0 ){
for(var i in selectedItems){
//var currItemID = selectedItems[i].id;
currItem = currList.getItemById(selectedItems[i].id);
cc.load(currItem);
cc.executeQueryAsync(function(sender, args){
var itemCreateInfo = new SP.ListItemCreationInformation();
var aListItem = currList.addItem(itemCreateInfo);
aListItem.set_item('Title', currItem.get_item('Title'));
aListItem.set_item('Customer', currItem.get_item('Customer'));
aListItem.set_item('Description', currItem.get_item('Description'));
aListItem.set_item('Source', currItem.get_item('Source'));
aListItem.set_item('field2', currItem.get_item('field2'));
aListItem.set_item('field3', currItem.get_item('field3'));
aListItem.set_item('Workloadtype', currItem.get_item('Workloadtype'));
aListItem.set_item('Tickettype', currItem.get_item('Tickettype'));
aListItem.set_item('Customergroup', currEngineer.group);
aListItem.set_item('Allocation', currEngineer.allocation);
aListItem.set_item('SubCap', currItem.get_item('SubCap'));
aListItem.set_item('Engineer', currEngineer.fullName);
aListItem.update();
cc.load(aListItem);
cc.executeQueryAsync(function(){
copyCounter ++;
},function(){
failedCounter ++;
});
}, Function.createDelegate(this,this.getItemFailed));
}
notifyMe();
}
}
与此同时,我找到了解决方案(重新思考这个问题很好)。
我用查询填充一个所需项目的数组,然后处理该数组。
var allSelectedItems;
function copySelected(){
if($("#copyAllButton").hasClass('ms-cui-disabled')){
return;
}
var cc = null;
var web = null;
copyCounter = 0;
failedCounter = 0;
cc = new SP.ClientContext.get_current();
web = cc.get_web();
//var currItem = null;
notifyId = SP.UI.Notify.addNotification(duplicatingItemsNotification, true);
var selectedItems;
currList = web.get_lists().getById(SP.ListOperation.Selection.getSelectedList());
selectedItems = SP.ListOperation.Selection.getSelectedItems();
if( selectedItems.length > 0 ){
allSelectedItems = new Array(selectedItems.length);
for(var i in selectedItems){
allSelectedItems[i] = currList.getItemById(selectedItems[i].id);
cc.load(allSelectedItems[i]);
}
cc.executeQueryAsync(Function.createDelegate(this, this.getItemSucceded), Function.createDelegate(this, this.getItemFailed));
notifyMe();
}
}