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?