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).
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?
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
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.
Have you looked at the SharePoint PNP Move-PnPFile command?
https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/move-pnpfile?view=sharepoint-ps
It has an option for OverwriteIfAlreadyExists.
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
Do you want to overwrite the file or create a new?
If FileName.PDF exists, create FileName2.PDF?
Yes, PowerShell PNP. Here’s a good document on how to get started:
https://collab365.community/perform-simple-crud-operations-sharepoint-pnp/
i want to overwrite this file sir
Did you try this: MoveCopyUtil.MoveFolder(srcContext, sourceFolder, destFolder, true);
I’m not at my work PC but I will try it tomorrow.
ok sir thanks and relax
this is my stack exchange for detail
https://sharepoint.stackexchange.com/questions/284496/copy-or-move-replace-file-and-update-version-sharepoint-csom
I was trying moving multiple folders and files. I am getting error File Size Limitation. What should I do ?
1) What version of SP?
2) What is the file size / how much data are you trying to copy?
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()
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.