Thursday, July 3, 2014

Copy a file across site collections with REST

I had to give the users the possibility to copy a file from the SharePoint search results to a document library in another site collection. This requirement resulted in the following custom display template: when clicking the 'Copy to other site collection' button, a div is expanded and shows a text box to provide a new filename and 2 radio buttons to select the target document library.



All the code is based on the example from Mikael Svenson. Since he is copying a file from appweb to hostweb he is using the SP.REquestExector javascript library. When I first read his article I thought this technique wasn't usable for my problem because I was going to copy files within the same domain.

Altough the SP.RequestExecutor library is a cross-domain javascript libary, this library is perfectly usable to use within the same domain. And it is fixed, so the patching described by Mikael isn't required anymore :)

This example was built for SharePoint 2013 online but works on premise as well.

23 comments:

  1. Really nice code, just what i needed! Thank you! Is it possible to get a look at the displaytemplate you used?

    ReplyDelete
  2. thanks, send me an email and I will get back to you with a preview.

    ReplyDelete
  3. Hello amazing sample, works just fine! Saved me a lot of time.

    Thank you, keep it up,

    VB

    ReplyDelete
  4. Useful blog...Does this code copy the document properties associated with the document ?

    ReplyDelete
    Replies
    1. No, but you can set the metadata afterwards. Please check this post: http://sanderlesage.blogspot.be/2014_12_01_archive.html
      I explain how you can change metadata of a new file.

      Delete
  5. When I run this code in O365 against an image (PNG, JPG) file, the file on the target site is corrupt (displays as a broken image). How did you overcome this issue?

    ReplyDelete
    Replies
    1. are you sure you've set property binaryStringRequestBody: true in both GET and POST request? I've had corrupted files as well before setting this property. Though, I've never tested with image files.

      Delete
    2. Yes, I'm certain. Images (and maybe binary fines in general) apparently require something else. I've even tried adding transformRequest: [] as a RequestExecutor parameter but the image always gets corrupted.

      Delete
    3. ok, hope I find some time to check this as well. Please let me know if you've found a solution :)

      Delete
  6. Did u find a solution for binary/image files?

    ReplyDelete
  7. Hi! I tried this but it throws the ret-error described in Mikael's blog... :/

    ReplyDelete
    Replies
    1. SharePoint Online, On-Prem? Sure your farm is up-to-date?

      Delete
  8. Hi, what are those parameters meant? specially the contextId, can you give me a more descriptive usage of this awesome function? any response will be great. Thank you!

    ReplyDelete
    Replies
    1. I had the same question... where do we get the "contextId"? There is no "ctxData" array in the page.

      Delete
  9. I have a situation where I need to give users the ability copy a whole document library to another site. Would that be as simple as putting this code in a loop or would I run into problems if I do that?

    ReplyDelete
    Replies
    1. hi Kees, take a look at the example described here: http://sharepoint.stackexchange.com/questions/114033/how-do-i-move-files-from-one-document-library-to-another-using-jsom

      Delete
  10. Hi, It is not working for me.. I am getting ret is not defined

    ReplyDelete
  11. Will the copy operation copies the metadatas and version history?

    ReplyDelete
  12. Tried the solution and created the file successfully but there's being a problem that the file created is in lock state, due to which i am not able to delete the file.
    If i run my code again to create the same file then it prompt and Error 423.

    ReplyDelete
  13. Great Post .
    I changed little bit in the code and it worked for me in Javascript.
    It amazing. ..
    :)

    ReplyDelete