0

I was create Ribbon Custom Action, and try to copy list items selected to same list, but F12 error? what is the problem?

enter image description here

function runCode() {
    var targetListItem;
    var values = SP.ListOperation.Selection.getSelectedItems();
    var clientContext = new SP.ClientContext.get_current();
    var listId = SP.ListOperation.Selection.getSelectedList();
    var targetList = clientContext.get_web().get_lists().getById(listId);
    targetListItem = targetList.getItemById(values[0].id);
    clientContext.load(targetListItem);
    clientContext.executeQueryAsync(function () {
        var itemCreateInfo = new SP.ListItemCreationInformation();
        var oListItem = targetList.addItem(itemCreateInfo); 
        oListItem.set_item('Title', targetListItem.get_item('Title'));  
        oListItem.update();
        clientContext.load(oListItem); 
        clientContext.executeQueryAsync(function(){
            SP.UI.Notify.addNotification('Item created!', false);
            SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);
       },Function.createDelegate(this, this.onQueryFailed));

   },Function.createDelegate(this, this.onQueryFailed));
   }

   function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }

Error:

Sys.ArgumentUndefinedException: The value cannot be null. Parameter name: method

  • Are you trying to copy items is same list?? – Ganesh Sanap May 30 at 10:19
  • Hello Ganesh Sanap, yes )) – Fox May 30 at 10:20
0

Try below Code:

var targetList = clientContext.get_web().get_lists().getById(listId);
clientContext.load(targetList);
targetListItem = targetList.getItemById(values[0].id);
clientContext.load(targetListItem, 'Title');

Reference:

  1. SP.List.getItemById Method
  • did not help (( – Fox May 30 at 10:26
  • Are you getting same error? – Ganesh Sanap May 30 at 10:27
  • Try this updated answer? are you getting anything in targetList?? – Ganesh Sanap May 30 at 10:34
0

Rewrote the code:

function getSelectedItems(OnSuccess, OnError) {
    var context = SP.ClientContext.get_current();
    var listId = SP.ListOperation.Selection.getSelectedList();
    var selectedItemIds = SP.ListOperation.Selection.getSelectedItems(context);
    var list = context.get_web().get_lists().getById(listId);
    var listItems = [];
    for (idx in selectedItemIds) {
        var item = list.getItemById(parseInt(selectedItemIds[idx].id));
        listItems.push(item);
        context.load(item);
    }
    context.executeQueryAsync(
       function () {
           OnSuccess(listItems);
       },
       OnError
    );
}
function copy() {
 var context = SP.ClientContext.get_current();
    var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
    var ListGUID = SP.ListOperation.Selection.getSelectedList();
    var clientContext = new SP.ClientContext.get_current();
    var targetList = clientContext.get_web().get_lists().getById(ListGUID);
    var itemCreateInfo = new SP.ListItemCreationInformation();
    getSelectedItems(function (items) {
        for (var i = 0 ; i < items.length; i++) {
        var oListItem = targetList.addItem(itemCreateInfo); 
        oListItem.set_item('Title', items[i].get_item('Title'));
        oListItem.update();
        clientContext.load(oListItem); 
        clientContext.executeQueryAsync();
        }
    },
        function (sender, args) {
            alert('An error occured: ' + args.get_message());
        });
    }
  • What was the exact issue causing error?? – Ganesh Sanap May 30 at 13:28
  • For some reason, the values of the selected items were null. Rewrote code differently. – Fox May 30 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.