Skip to main content

Copy files from a document library to a files system by looping through all folders and subfolders in a library

If you have a need to copy file files from a document library to a SharePoint files system specifically the layouts folder.

     private void LoopSubFolders(string folderPath, SPFolderCollection subFolders)
        {
            string templatePath = SPUtility.GetGenericSetupPath("TEMPLATE");
            string FolderPath = Path.Combine(templatePath, @"LAYOUTS\ATP\");
            try
            {
                foreach (SPFolder folder in subFolders)
                {
                    if (folder.Name.ToLower() != "forms")
                    {
                        string folderName = folderPath + @"\" + folder.Name;
                        if (!Directory.Exists(FolderPath + folderName))
                            Directory.CreateDirectory(FolderPath + folderName);
                        foreach (SPFile item in folder.Files)
                        {
                            DownloadFileUsingFileStream(item.Item.File, FolderPath + folderName + @"\");
                        }
                        this.recurSubFolders(folderName, folder.SubFolders);
                    }
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text += ex.ToString();
            }
        }

QED

Comments