Save files from SharePoint document library to File System

Normally you would get requirements for uploading documents to the document library, but you would rarely find the requirement to download all the documents from SharePoint List to the the file system. As such the functionality is pretty easy to implement -> Read the Byte stream of the document library item and then Write the stream using any writer.

Lets add a bit of fun in this. Consider the document library has documents at multiple levels and folders at each levels which have documents and further folders in them.

The only solution for this is to use the Cool Principle of "RECURSION". Below is the code I have written.

 using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace CA_FileCopyFromDocLibToserver
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Please enter the url of the site collection (Press Enter after typing)");
                SPSite site = new SPSite(Console.ReadLine());

                SPWeb web = site.AllWebs[""];

                Console.WriteLine("Please enter the Document Library Name (Press Enter after typing)");
                string docLibName = Console.ReadLine();

                Console.WriteLine("Please enter the Target Url on the machine (Press Enter after typing)");
                string TargetUrl = Console.ReadLine();

                CopyFolder(web, docLibName, TargetUrl);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error --- " + ex.Message);
                Console.ReadLine();
            }
            Console.WriteLine("Files Copied Succesfully.");
            Console.ReadLine();
        }

        static public void CopyFolder(SPWeb web, string sourceFolder, string destFolder)
        {
            SPFolder spSourceFolder = web.GetFolder(sourceFolder);
            
            if (!Directory.Exists(destFolder))
                Directory.CreateDirectory(destFolder);
            SPFileCollection files = spSourceFolder.Files;
            foreach (SPFile file in files)
            {
                byte[] b = file.OpenBinary();
                FileStream fs = new FileStream(destFolder + "\\" + file.Name, FileMode.Create, FileAccess.ReadWrite);
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Write(b);
                bw.Close();
            }

            SPFolderCollection folders = spSourceFolder.SubFolders;
            
            foreach (SPFolder folder in folders)
            {
                
                string name = folder.Name;
                string dest = Path.Combine(destFolder, name);
                if (folder.Name != "Forms")
                {
                    CopyFolder(web, folder.Url, dest);
                }
            }
        }

    }
}

One small important thing to remember here is that when you iterate recursively through the list of folders of the SharePoint Document library it will also pick up the Forms folder which has OOB files such newform.aspx, etc. which may not be required to be downloaded as it is not part of the content.