This post is intended for SharePoint Online and 2016+ using CSOM.
In previous version of SharePoint, moving a folder structure to a different library or a different library in another site collection required a good amount of code and effort. With the introduction of Microsoft.SharePoint.Client.MoveCopyUtil we now have the ability to use methods like MoveFolder to perform a cut and paste of a folder structure. There are other methods that are worth exploring like FolderCopy that is a copy and paste of a structure.
More methods can be found here: MoveCopyUtil members
Name | Description | |
---|---|---|
![]() ![]() |
CopyFile | |
![]() ![]() |
CopyFileByPath | |
![]() ![]() |
CopyFolder | |
![]() ![]() |
CopyFolderByPath | |
![]() ![]() |
MoveFile | |
![]() ![]() |
MoveFileByPath | |
![]() ![]() |
MoveFolder | |
![]() ![]() |
MoveFolderByPath |
Example of moving a folder between site collections.
string dstUrl = "https://sharepoint.com/sites/B/"; using (ClientContext srcContext = new ClientContext(dstUrl)) { string sourceFolder = "https://sharepoint.com/sites/A/libraryname/foldername"; string destFolder = dstUrl + "libraryname/foldername"; MoveCopyUtil.MoveFolder(srcContext, sourceFolder, destFolder); srcContext.ExecuteQuery(); }Again, this is using CSOM with the latest version of the NuGet package (16.0.4351.1000).