6

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 ?

enter image description here

2

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;
    }
  • What value shouls i pass for token when i am calling this method? – SP Enthusiast Nov 3 '15 at 10:54
  • You can eliminate token parameter. – Kaushal15 Nov 3 '15 at 12:47
  • Yes, I have eliminated the token parameter. Still the solution is not working. Throws an exception at currentweb.GetFolderByServerRelativeUrl(source) part. Can you please help me to find the exact solution for my question? – SP Enthusiast Nov 3 '15 at 15:43
  • What did you pass in url and source? for url pass "" (blank) and for source "/Shared%20documents/Expenses" (/libraryname/foldername) – Kaushal15 Nov 4 '15 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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