Copy or Move Folder Structure in SharePoint Using CSOM

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
Public method Static member CopyFile
Public method Static member CopyFileByPath
Public method Static member CopyFolder
Public method Static member CopyFolderByPath
Public method Static member MoveFile
Public method Static member MoveFileByPath
Public method Static member MoveFolder
Public method Static member 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).

13 thoughts on “Copy or Move Folder Structure in SharePoint Using CSOM

  1. Hi, I was googling copying folder structure to SharePoint and I found this page. However, in my particular case, I want to copy a folder structure form a shared folder location in my local network.
    Can you please tell me if there is a similar procedure that uses the shared folder path \\server\shareFolder\folderStructure to copy the folder structure to a SharePoint library?

  2. This post on stackexchange will get you headed in the right direction. Link: https://sharepoint.stackexchange.com/questions/208425/powershell-script-to-transfer-files-from-local-folder-to-document-library

    It’s a little more involved than simply copying folders around SP but it’s fairly straightforward. Also, be sure to check out what SharePoint PNP has to offer for copying files from your serer to SP.

    Link: https://docs.microsoft.com/en-us/powershell/sharepoint/sharepoint-pnp/sharepoint-pnp-cmdlets?view=sharepoint-ps

  3. hi i want to ask about if we found duplicate name file or folder it’s posible to replace new version? so replace to version 2.

  4. hi ian it’s on power shell?how to do like that on CSOM?when i try to

    MoveCopyOptions option = new MoveCopyOptions();
    option.KeepBoth = true;
    MoveCopyUtil.CopyFile(this.clientContext, SrcUrl, DestUrl, true, option);
    this.clientContext.ExecuteQuery();

    overwrite is true but still not and getting error destination exists work to overwrite my Src is

    http://win-e636ggi1v13:55555/sites/srsrms/SRS%20Documents/Finance/fredytest/License%20Management.csv

    to

    http://win-e636ggi1v13:55555/sites/srsrms/SRS%20Documents/Finance/paidi/Finance%20Folder/License%20Management.csv

  5. Did you try this: MoveCopyUtil.MoveFolder(srcContext, sourceFolder, destFolder, true);

    I’m not at my work PC but I will try it tomorrow.

  6. I was trying moving multiple folders and files. I am getting error File Size Limitation. What should I do ?

  7. Hello,
    I just tried your simple code in my SharePoint OnPrem environment. “MoveCopyUtil.MoveFolder” is working in same site collection. But When try the move folder diffrent site collection, I am getting this error
    Microsoft.SharePoint.Client.ServerException
    HResult=0x80131500
    Message=MountPoint security error: NoScript isn’t enabled on the host site
    Source=Microsoft.SharePoint.Client.Runtime
    StackTrace:
    at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)
    at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
    at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb)
    at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()

  8. It looks like the error points to your problem.

    The error message suggests that the NoScript feature isn’t enabled on the SharePoint host site, and this is causing a security error. This could be restricting the operation you are trying to perform with MoveCopyUtil.MoveFolder.

    Here are some steps you might consider:

    1. **Contact Admin**: You’ll likely need administrative privileges to change this setting. If you’re not the admin, reach out to them.

    2. **Enable NoScript**: If you have admin access, you might need to enable the NoScript setting for the SharePoint host site.

    3. **Review Permissions**: Ensure that the user or service account running the operation has the necessary permissions to perform the task.

    4. **Update Code**: If you have control over the code invoking MoveCopyUtil.MoveFolder, make sure it adheres to the security policies set by the host site.

    5. **Consult Documentation**: Check Microsoft’s official documentation for specifics on the NoScript feature and how it affects operations like MoveFolder.

Leave a Reply

Your email address will not be published. Required fields are marked *