2

I'm trying to make a copy of a MS Word document and add it to a document library in SharePoint Online with the below code, which runs once without firing the success or fail message, and throws "SP.RequestExecutor is not a constructor" on subsequent executions. I have jQuery referenced on the master page.

What am I missing?

function copy() {
  var hostweburl = decodeURIComponent("https://myWeb.sharepoint.com"); 
  var appweburl = decodeURIComponent("https://myWeb.sharepoint.com/mySite"); 

  $.getScript(hostweburl + "/_layouts/15/SP.RequestExecutor.js");

  var executor = new SP.RequestExecutor(appweburl);
  var url = "https://myWeb.sharepoint.com/mySite/_api/web/folders/getByUrl('mySite/Deviations/Forms/Deviations/')/Files/getByUrl('template.docx')/copyTo(strNewUrl = 'mySite/Deviations/myTestDev.docx', bOverWrite = true)";
  executor.executeAsync({
    url: url,
    method: "POST",
    binaryStringResponseBody: true,
    success: successHandler,
    error: errorHandler
  });

  function successHandler() {
    console.log('success');
  };

  function errorHandler(errMsg) {
    console.log('fail ' + errMsg);
  };
}}

2 Answers 2

5

Have you tried moving your code into the jQuery.getScript() success callback?
Like this:

$.getScript(hostweburl + "/_layouts/15/SP.RequestExecutor.js", function() {
  var executor = new SP.RequestExecutor(appweburl);
  // rest of code...
});
0
0

You can try the following code for copying file from one location to another within SharePoint.

The following example will be helpful in copying files within SharePoint sandbox.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myapp" ng-controller="mycont">
    <input type="button" ng-click = "myclick()" value="test Man" /> 
</div>

<input type=button onclick="x()"  value="copyfile" />
<script>
    var dt =new Date();
    var val_ue = dt.getDate()+""+dt.getHours()+""+dt.getMinutes()+""+dt.getSeconds() +"1" ;
    var url1 = "/_api/web/getfilebyserverrelativeurl('/Lists/Document_Mapping/Attachments/1/9.jpg')";
    var url2 = "/Lists/AddressVersioning/Attachments/84/" ;
    var combined = "";
    var app = angular.module('myapp',[]);
    var _headers = {
        'X-RequestDigest': document.getElementById("__REQUESTDIGEST").value,
        'accept':'application/json;odata=verbose'
    };
    app.controller('mycont',function($scope,$http){
        $scope.myclick =  function(){
            combined = url1 + "/copyTo('" + url2 + val_ue + ".jpg')";
            $http({method:'POST',url:combined,headers:_headers}).then(
                function(response){
                    console.log("hi");
                    val_ue += 1;
                },
                function(error){
                    console.log("Error:");
                    console.log(error);
                },
                function(process){
                    console.log("process:");
                    console.log(process);
                }
            );
        }
    });
    var x = function(){
        combined = url1 + "/copyTo('" + url2 + val_ue + ".jpg')";
        $.ajax({
            url : combined,
            method: 'POST',
            headers: {
                "Accept": "application/json; odata=verbose",
                "X-RequestDigest":  $("#__REQUESTDIGEST").val()
            },
            success: function () {
                alert("Success! Your file was copied properly");
                val_ue +=1;
            },
            error: function () {
                alert("Problem with copying");
            }
        });
    }
</script>

Note: the above function will not work if the list item is newly created. But for all other situations it will work (even form one doc library to another doc library or cross site / site collection)

Your Answer

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

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