Download all Files from SharePoint Document Library

Using Power shell command use below code :

  1. ######################## Start Variables ########################  
  2. ######################## Varun's Script######################  
  3. $destination = "C:\\tools\\Folder"  
  4. $webUrl = "<Url of the specific site>"  
  5. $listUrl = "<Url of the specific list. This url is complete Url and NOT relative url>"  
  6. ##############################################################  
  7. $web = Get - SPWeb - Identity $webUrl  
  8. $list = $web.GetList($listUrl)  
  9.   
  10. function ProcessFolder {  
  11.     param($folderUrl)  
  12.     $folder = $web.GetFolder($folderUrl)  
  13.     foreach($file in $folder.Files) {#  
  14.         Ensure destination directory  
  15.         $destinationfolder = $destination + "/" + $folder.Url  
  16.         if (!(Test - Path - path $destinationfolder)) {  
  17.             $dest = New - Item $destinationfolder - type directory  
  18.         }#  
  19.         Download file  
  20.         $binary = $file.OpenBinary()  
  21.         $stream = New - Object System.IO.FileStream($destinationfolder + "/" + $file.Name), Create  
  22.         $writer = New - Object System.IO.BinaryWriter($stream)  
  23.         $writer.write($binary)  
  24.         $writer.Close()  
  25.     }  
  26. }#  
  27. Download root files  
  28. ProcessFolder($list.RootFolder.Url)# Download files in folders  
  29. foreach($folder in $list.Folders) {  
  30.     ProcessFolder($folder.Url)  
  31. }  
Using Server object model use below code:
  1. static void Main(string[] args) {  
  2.     using(SPSite site = new SPSite("http://site name")) {  
  3.         using(SPWeb web = site.OpenWeb()) {  
  4.             // Library name - Shared Documents  
  5.             SPList list = web.Lists["Shared Documents"];  
  6.             processFolder(list.RootFolder.Url);  
  7.             foreach(SPFolder folder in list.RootFolder.SubFolders) {  
  8.                 processFolder(folder.Url);  
  9.             }  
  10.         }  
  11.     }  
  12. }  
  13. public static void processFolder(string folderURL) {  
  14.     string Destination = @  
  15.     "c:\\temp";  
  16.     using(SPSite site = new SPSite("http://yoursite")) {  
  17.         using(SPWeb web = site.OpenWeb()) {  
  18.             SPFolder folder = web.GetFolder(folderURL);  
  19.             foreach(SPFile file in folder.Files) {  
  20.                 string destinationfolder = Destination + "/" + folder.Url;  
  21.                 byte[] binary = file.OpenBinary();  
  22.                 if (!Directory.Exists(destinationfolder)) {  
  23.                     Directory.CreateDirectory(destinationfolder);  
  24.                 }  
  25.                 FileStream stream = new FileStream(destinationfolder + "/" + file.Name, FileMode.Create);  
  26.                 BinaryWriter writer = new BinaryWriter(stream);  
  27.                 writer.Write(binary);  
  28.                 writer.Close();  
  29.             }  
  30.         }  
  31.     }  
  32. }  
Using Client object model use below code:
  1. static void Main(string[] args) {  
  2.     var site = new ClientContext("http://sitename/");  
  3.     var web = site.Web;  
  4.     site.Load(web);  
  5.     site.ExecuteQuery();  
  6.     // Library name - Shared Documents   
  7.     List list = web.Lists.GetByTitle("Shared Documents");  
  8.     site.Load(list);  
  9.     site.ExecuteQuery();  
  10.     site.Load(list.RootFolder);  
  11.     site.ExecuteQuery();  
  12.     site.Load(list.RootFolder.Folders);  
  13.     site.ExecuteQuery();  
  14.     processFolderClientobj(list.RootFolder.ServerRelativeUrl);  
  15.     foreach(Folder folder in list.RootFolder.Folders) {  
  16.         processFolderClientobj(folder.ServerRelativeUrl);  
  17.     }  
  18. }  
  19.   
  20. public static void processFolderClientobj(string folderURL) {  
  21.     string Destination = @  
  22.     "c:\\temp";  
  23.     var site = new ClientContext("http://sitename/");  
  24.     var web = site.Web;  
  25.     site.Load(web);  
  26.     site.ExecuteQuery();  
  27.     Folder folder = web.GetFolderByServerRelativeUrl(folderURL);  
  28.     site.Load(folder);  
  29.     site.ExecuteQuery();  
  30.     site.Load(folder.Files);  
  31.     site.ExecuteQuery();  
  32.     foreach(Microsoft.SharePoint.Client.File file in folder.Files) {  
  33.         string destinationfolder = Destination + "/" + folder.ServerRelativeUrl;  
  34.         Stream fs = Microsoft.SharePoint.Client.File.OpenBinaryDirect(site, file.ServerRelativeUrl).Stream;  
  35.         byte[] binary = ReadFully(fs);  
  36.         if (!Directory.Exists(destinationfolder)) {  
  37.             Directory.CreateDirectory(destinationfolder);  
  38.         }  
  39.         FileStream stream = new FileStream(destinationfolder + "/" + file.Name, FileMode.Create);  
  40.         BinaryWriter writer = new BinaryWriter(stream);  
  41.         writer.Write(binary);  
  42.         writer.Close();  
  43.     }  
  44. }  
  45. public static byte[] ReadFully(Stream input) {  
  46.     byte[] buffer = new byte[16 * 1024];  
  47.     using(MemoryStream ms = new MemoryStream()) {  
  48.         int read;  
  49.         while ((read = input.Read(buffer, 0, buffer.Length)) > 0) {  
  50.             ms.Write(buffer, 0, read);  
  51.         }  
  52.         return ms.ToArray();  
  53.     }  
  54. }