2

I wan't to copy a file from one library to another and set some field values. I used the following code to perform the file copy:

function copyFileREST(name) {

var call = $.ajax({
 url : "https://myURL/sites/_api/web/folders/GetByUrl('SourceList')/Files/getbyurl('sourceFilename.xlsm')/copyTo(strNewUrl='DestinationList/" + name + "',bOverWrite=false)?$expand=ListItemAllFields",

method: 'POST',
headers: {
    "Accept": "application/json; odata=verbose",
    "X-RequestDigest":  $("#__REQUESTDIGEST").val()
},
success: function () {
    console.log("Success! Your file was copied properly");
},
error: function () {
    console.log("Problem with copying");
}
});
console.log("copy ended");
 return call;
}

After the file is copied successfully I do need to update some field values. Is there a way I get back the itemID of the file I just copied? I found some examples when uploading a file but it seems that this is not working for copyTo method.

Any ideas?

1 Answer 1

2

This will require a few different calls. One to copy the item, one to get the item Id for the copied item, and then to update the copied item's properties. Below should get you started:

var copyFileWithProperties = function(name, updateData) {
    var d = $.Deferred();
    $.ajax({
        url: "https://myURL/sites/_api/web/folders/GetByUrl('SourceList')/Files/getbyurl('sourceFilename.xlsm')/copyTo(strNewUrl='DestinationList/" + name + "',bOverWrite=false)?$expand=ListItemAllFields",

        type: 'POST',
        headers: {
            "Accept": "application/json; odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function () {
            d.notify("Success! Your file was copied properly");
            var oDataUrl = "https://myURL/sites/_api/web/GetFileByServerRelativeUrl('DestinationList/" + name + "')/ListItemAllFields?$select=Id";
            $.ajax( {
                url : oDataUrl,
                type : "GET",
                headers : {
                    "Accept" : "application/json; odata=verbose",
                    "X-RequestDigest" : $( "#__REQUESTDIGEST" ).val()
                },
                success : function( data ) {
                    d.notify( "Retrieved new item id" );
                    oDataUrl = "https://myURL/sites/_api/web/lists/getbytitle('Destination List')/items(" + data.d.Id + ")"; // Double check what's in data, I forget the format of the return object for verbose
                    $.ajax( {
                        url : oDataUrl,
                        type : "POST",
                        headers : {
                            "X-HTTP-Method" : "MERGE",
                            "Accept" : "application/json; odata=verbose",
                            "X-RequestDigest" : $( "#__REQUESTDIGEST" ).val(),
                            "IF-MATCH" : "*"
                        },
                        data : updateData,
                        success : function() {
                            d.resolve( "File uploaded and properties were updated." );
                        },
                        error : function( eData ) {
                            d.reject( "Error updating file properties.", eData );
                        }
                    } );
                },
                error : function( eData ) {
                    d.reject( "Error retrieving the copied file from the destination list.", eData );
                }
            } );
        },
        error: function (eData) {
            d.reject("Problem with copying", eData);
        }
    });
    return d.promise();
}

var call = copyFileWithProperties(name, updateData);
call.then(
    function(successMessage) { console.log(successMessage); },
    function(errorMessage, errorData) { console.error(errorMessage, errorData); },
    function(progressMessage) { console.log(progressMessage); }
);

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.