Mar 17 2017 02:34 AM
Hi,
I'm looking for some samples using PnP js to copy documents between libraries in SPO.
(I've tries Binging etc...)
Any?
Mar 19 2017 03:31 AM - edited Mar 19 2017 03:34 AM
SolutionHi 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
Mar 21 2017 04:14 AM
Thanks!
A second question...is it possible to use pnp-core-js to copy file(s) to a doc lib in another sitecollection, too?
Mar 21 2017 04:36 AM
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.
Mar 24 2017 05:44 AM
Thanks.
Just would be nice to not transfer the document to the client and back. But we then have to make the solution in Azure...
Feb 07 2018 03:33 AM
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!!
Feb 08 2018 09:04 AM
bump - any progress on this ?
Feb 19 2021 09:02 AM
Thank you for your answer
I used your method with sp pnp like below
Mar 19 2017 03:31 AM - edited Mar 19 2017 03:34 AM
SolutionHi 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