1

I want to copy multiple files from one Document Library to another with metadata and column values associated from the copied file to the destination file. I am using SharePoint 2013 JavaScript Client Object Model. I referred to the code at this https://sharepoint.stackexchange.com/a/100327 link, but I am unable to get Item IDs of newly copied files.

1 Answer 1

1

copyTo method don't return any values. Hence I believe you need to get newly created file as below

 _destinationlibUrl = web.get_serverRelativeUrl() +'/PageArchive/' + _sourceFile.get_name();
 _sourceFile.copyTo(_destinationlibUrl, 1);

 _ctx.executeQueryAsync(
    function (sender, args) {
      var url = _destinationlibUrl .replace(_spPageContextInfo.siteAbsoluteUrl,'');    
      var newFile = _ctx.get_web().getFileByServerRelativeUrl(url);   
      _ctx.load(newFile,'ListItemAllFields'); 
      _ctx.executeQueryAsync(
            function () {
                  alert(newFile.get_id());
            }, 
            function () {
                  alert("Failed");
            }
       );
    },
    function (sender, args) {
        alert('Error occured: ' + args.get_message());
    }
2
  • The file is getting copied but giving error at---- alert(newFile.get_id());---- Error Message: Uncaught TypeError: newFile.get_id is not a function
    – Yayati
    Jun 1, 2015 at 5:19
  • the line alert(newFile.get_id()); should be replaced by alert(newFile.get_listItemAllFields().get_id()); Jun 1, 2015 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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