3

I have a library like below :

List
---------Folder 1
---------File 0
---------File 1
-----------------Folder 2
------------------------File 2
------------------------File 3
---------Folder 3
-----------------File 4
-----------------File 5

The plan is to copy it to another library, i have found a way to copy the File 0 / File 1. But i'm looking for a way to copy all the library (folder with subfolder and file inside) to my other library.

To copy the file i did this:

SPListItem item = oldLib.GetItemById(collItem[i].ID);
byte[] fileBytes = item.File.OpenBinary();
string destUrl = newlib.RootFolder.Url + "/" + item.File.Name;
SPFile destFile = newlib.RootFolder.Files.Add(destUrl, fileBytes, true);

Did i miss an easy way to do what i want?

Best regards

1

You can use SPFile CopyTo / MoveTo methods.

file.CopyTo(destUrl, true);

| improve this answer | |
0

You can use SP.MoveCopyUtil.moveFolder() method to Move Folder From one Document Library to another Document Library. Instead of Coping file from one document library to another.
Try to use Following code, It's work for me :-

function moveFolder() {
  ExecuteOrDelayUntilScriptLoaded(function () {
  var context = SP.ClientContext.get_current();
  SP.MoveCopyUtil.moveFolder(context, 
  "https://xxx.sharepoint.com/DocumentLibraryOne/Folder", 
  "https://xxx.sharepoint.com/DocumentLibraryTwo/Folder");

   context.executeQueryAsync(function () {
        alert("success.");
    }, function (sender, args) {
           alert('request failed ' + args.get_message() + '\n' 
 +args.get_stackTrace());
});
}, "sp.js");
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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