How to copy the entire source folder to target folder within same document library using c# and CSOM?
-
06-10-2020 - |
Question
I want to copy the entire source folder to target folder within same document library using c# and CSOM
I've folder structure like in Shared documents/Expenses will be the source folder. the target would be Shared documents/2015/. I've to copy all folders including root folder expenses with files to Shared documents/2015/Expenses
CODE:
public static void CopyFiles(string url, string listTitle, string srcFolder,string destFolder)
{
using (var context = new ClientContext(url))
{
//context.Credentials = credentials;
var srcList = context.Web.Lists.GetByTitle(listTitle);
var qry = CamlQuery.CreateAllItemsQuery();
qry.FolderServerRelativeUrl = string.Format("{0}", srcFolder);
var srcItems = srcList.GetItems(qry);
context.Load(srcItems, icol => icol.Include(i => i.FileSystemObjectType, i => i["FileRef"], i => i.File));
context.ExecuteQuery();
foreach (var item in srcItems)
{
switch (item.FileSystemObjectType)
{
case FileSystemObjectType.Folder:
var destFolderUrl = ((string)item["FileRef"]).Replace(srcFolder,destFolder);
CreateFolder(context.Web,destFolderUrl);
break;
case FileSystemObjectType.File:
var destFileUrl = item.File.ServerRelativeUrl.Replace(srcFolder, destFolder);
item.File.CopyTo(destFileUrl, true);
context.ExecuteQuery();
break;
}
}
}
}
private static Folder CreateFolder(Web web, string folderUrl)
{
if (string.IsNullOrEmpty(folderUrl))
throw new ArgumentNullException("Folder Url could not be empty");
var folder = web.Folders.Add(folderUrl);
web.Context.Load(folder);
web.Context.ExecuteQuery();
return folder;
}`
But it is not copying the root folder. Instead it is copying all folders and files inside root folder. Can someone help what I'm missing in this ?
Solution
You can try out this way:
public bool PasteFolder(string url, string source, string sourceListName, string target, string targetListName, string targetFolderName, string token)
{
bool isFolderPasted = false;
try
{
using (var context = new ClientContext(url))
{
//context.Credentials = credentials;
var currentWeb = context.Site.OpenWeb(url);
var addFoldername = targetFolderName;
var sourceFolder = currentWeb.GetFolderByServerRelativeUrl(source);
context.Load(sourceFolder.Folders);
context.Load(sourceFolder.Files);
context.ExecuteQuery();
Folder newfolder;
try
{
newfolder = currentWeb.GetFolderByServerRelativeUrl(target);
context.Load(newfolder);
context.ExecuteQuery();
}
catch
{
target = target.Replace("/", string.Empty).Trim();
var list = currentWeb.Lists.GetByTitle(target);
newfolder = list.RootFolder;
context.Load(newfolder);
context.ExecuteQuery();
}
var currentFolder = newfolder.Folders.Add(addFoldername);
context.Load(currentFolder);
context.Load(currentFolder.Folders);
context.Load(currentFolder.Files);
context.ExecuteQuery();
foreach (var file in sourceFolder.Files)
{
file.CopyTo(currentFolder.ServerRelativeUrl + "/" + file.Name, true);
context.ExecuteQuery();
}
foreach (var cFolder in sourceFolder.Folders)
{
PasteFolder(url, cFolder.ServerRelativeUrl, sourceListName, currentFolder.ServerRelativeUrl, targetListName, cFolder.Name, token);
}
isFolderPasted = true;
}
}
catch (Exception ex)
{
}
return isFolderPasted;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange