Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to copy file between document libraries. Library A is located in one site and Library B is located in subsite. I know how to copy file between libraries on the same level but the problem is with copying between different level.

The code I use to copy file between libraries on the same level.

 $.ajax({
     url : "http://xxx/PWA/_api/web/folders/GetByUrl('/PWA/CopyFromLibrary')/Files/getbyurl('Import.csv')/copyTo(strNewUrl = '/PWA/TargetLibrary/Import.csv',bOverWrite = true)",

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

For different level I use just another target URL:

url : "http://xxx/PWA/_api/web/folders/GetByUrl('/PWA/CopyFromLibrary')/Files/getbyurl('Import.csv')/copyTo(strNewUrl = '/PWA/Subsite/TargetLibrary/Import.csv',bOverWrite = true)",

And it doesn't work. How to work around this problem?

share|improve this question
add comment

1 Answer

What kind of error are you getting?

One probable cause of your problem is that your RequestDigest does not match the location where you want to POST your file since it is fetched from the page where your code is running. Fetch a matching RequestDigest by calling '_api/contextinfo' on your target location.

See: http://blogs.breeze.net/mickb/2012/11/20/SP2013GettingAFormDigestForUpdateRESTCalls.aspx and http://msdn.microsoft.com/en-us/magazine/dn198245.aspx (writing to Sharepoint section)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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