0

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.

1 Answer 1

0

Try this:

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.