0

I have a Folder whose folder structure must be cloned to another folder in another document library.

Is it possible to do something like using file/folder REST API

/_api/web/getfolderbyserverrelativeurl('{0}')/folder/copyto(destinationpath)

or List REST API

/_api/web/lists(guid'{0}')/items({1})/copyto(destiation list/item)

2 Answers 2

1

Yes it is possible using this endpoint: site/_api/SP.MoveCopyUtil.CopyFolderByPath() and the right parameters as you can see below:

var postData = {
   "srcPath":{
      "__metadata":{
         "type":"SP.ResourcePath"
      },
      "DecodedUrl":"https://example.com/sites/<siteName>/folder/<FolderToCopy>"
   },
   "destPath":{
      "__metadata":{
         "type":"SP.ResourcePath"
      },
      "DecodedUrl":"https://example.com/sites/<siteName>/folder/<NameOfNewFolder>"
   },
   "options":{
      "__metadata":{
         "type":"SP.MoveCopyOptions"
      },
      "ResetAuthorAndCreatedOnCopy":true,
      "ShouldBypassSharedLocks":true
   }
};
$.ajax({
        type: "POST",
        url: "https://example.com/sites/<siteName>/_api/SP.MoveCopyUtil.CopyFolderByPath()",
        data: JSON.stringify(postData),
        headers: {
            "Accept": "application/json; odata=verbose",
            "X-RequestDigest":  $("#__REQUESTDIGEST").val()
        },
        contentType: "application/json; odata=verbose",
        success: function (data) {
            console.log(data);
        }
});

I hope this will help someone in the future.

1
  • Any option to skip the existing folder if any in the hierarchy?
    – Sudhir
    Commented Nov 24, 2021 at 11:02
0

I haven`t tested the complete code but seems should work for your scenario.

<<web url>>/_api/web/getfilebyserverrelativeurl('/subsite/Shared%20Documents/sample.txt')/copyTo('/subsite/Shared%20Documents/sample1.txt')

The above one is POST end point.

I don`t see any CopyTo end point for folder.

https://social.msdn.microsoft.com/Forums/vstudio/en-US/dff2a79e-c041-4f7a-9890-d0955705a1ed/problem-copying-file-using-rest-api-method-copyto?forum=sharepointdevelopment

MSDN reference for Folder and File end points

https://msdn.microsoft.com/en-us/library/office/dn450841.aspx#bk_Folder

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.