Forum Discussion

Jon Ivar Kleveland's avatar
Jon Ivar Kleveland
Copper Contributor
Mar 17, 2017
Solved

Copy documents between libraries with js pnp

Hi,

I'm looking for some samples using PnP js to copy documents between libraries in SPO.

(I've tries Binging etc...)

 

 

Any?

  • Hi there,

    It can be done with `copyTo` method. With pnp-core-js:

     

    $pnp.sp.web
      .getFileByServerRelativeUrl('/sites/dev01/DocLib01/tttt/Development and Deployment Guidelines.pdf')
      .copyTo('/sites/dev01/DocLib03/Development and Deployment Guidelines.pdf', true)
      .then(function(res) { 
        console.log(res); 
      });

     

    Which constructs POST request to the following REST endpoint:

     

    /_api/web
      /getfilebyserverrelativeurl('[server relative url of the file to copy')
      /copyto(
    strnewurl='[server reletive url where to copy, including file name]',
    boverwrite=true)

     

    --

    Cheers,

    Andrew

7 Replies

  • Hi there,

    It can be done with `copyTo` method. With pnp-core-js:

     

    $pnp.sp.web
      .getFileByServerRelativeUrl('/sites/dev01/DocLib01/tttt/Development and Deployment Guidelines.pdf')
      .copyTo('/sites/dev01/DocLib03/Development and Deployment Guidelines.pdf', true)
      .then(function(res) { 
        console.log(res); 
      });

     

    Which constructs POST request to the following REST endpoint:

     

    /_api/web
      /getfilebyserverrelativeurl('[server relative url of the file to copy')
      /copyto(
    strnewurl='[server reletive url where to copy, including file name]',
    boverwrite=true)

     

    --

    Cheers,

    Andrew

    • brainstorm160's avatar
      brainstorm160
      Copper Contributor

      Andrew Koltyakov 

      Thank you for your answer

      I used your method with sp pnp like below

        sp.web.getFileByServerRelativeUrl(attachments.ServerRelativeUrl)
                        .copyTo('(siteName)/TestDocuments/',false)
                            .then(result => {
                              console.log("Uploaded Document", result)
                            })
          However, it is constructed Get request not Post.  As a result I face the following error message
       
      The HTTP method 'GET' cannot be used to access the resource 'CopyTo'. The operation type of the resource is specified as 'Default'. Please use correct HTTP method to invoke the resource
       
      How can I fix this issue?
       
      Thank you so much ahead                
    • Vandana Chadha's avatar
      Vandana Chadha
      Copper Contributor

      Hello,

       

      How can we maintain versioning in the target library while copying the files from Source to target, if the target library has versioning enable. Please suggest!

      Thanks!!

    • Jon Ivar Kleveland's avatar
      Jon Ivar Kleveland
      Copper Contributor

      Thanks!

       

      A second question...is it possible to use pnp-core-js to copy file(s) to a doc lib in another sitecollection, too?

      • Andrew Koltyakov's avatar
        Andrew Koltyakov
        MVP

        Hi Jon,

         

        Nope, it doesn't seem to be possible with REST.

        CopyTo method works within single SPWeb.

         

        Though, it's possible to get a file's blob/buffer and metadata and create a new document in other SPSite/SPWeb using REST and pnp js core.

Resources