0

在 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();
}

}

4

1 回答 1

1

与此同时,我找到了解决方案(重新考虑问题的问题很好)。

我用查询填充所需项目的数组,然后处理该数组。

 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();
}

}

于 2013-08-28T07:49:51.653 回答