I want to copy files from one document library folder to another document library folder. here is my code.
using (var clientContext = new ClientContext(webUrl))
{
clientContext.Credentials = CredentialCache.DefaultNetworkCredentials;
clientContext.Load(clientContext.Web);
List sourceList = clientContext.Web.Lists.GetByTitle("Hassam_Folder");
CamlQuery caml = CamlQuery.CreateAllFoldersQuery();
Folder rootFolder = sourceList.RootFolder;
FolderCollection folders = rootFolder.Folders;
clientContext.Load(folders);
clientContext.ExecuteQuery();
using (var destinationContext = new ClientContext(webUrl))
{
clientContext.Credentials = CredentialCache.DefaultNetworkCredentials;
clientContext.Load(clientContext.Web);
List DestinationList = destinationContext.Web.Lists.GetByTitle("Ahsan_Folder");
Folder rootFolderdest = sourceList.RootFolder;
FolderCollection destfolders = rootFolder.Folders;
if (folders.Count != 0)
{
foreach (Folder folder in folders)
{
clientContext.Load(folder.Files);
FileCollection sourceFile = folder.Files ;
clientContext.Load(sourceFile);
clientContext.ExecuteQuery();
Folder destRootFolder = DestinationList.RootFolder;
FolderCollection foldertocreate = destRootFolder.Folders;
foldertocreate.Add(folder.Name);
DestinationList.Update();
destinationContext.Load(foldertocreate);
destinationContext.ExecuteQuery();
}
}
}
}
This code create folder with same name to destination document library. Now i Have to add files in destination folders with versions but did not get any idea? any help should be appreciated thanks.