Using Power shell command use below code
- $destination = "C:\\tools\\Folder"
- $webUrl = "<Url of the specific site>"
- $listUrl = "<Url of the specific list. This url is complete Url and NOT relative url>"
- $web = Get - SPWeb - Identity $webUrl
- $list = $web.GetList($listUrl)
-
- function ProcessFolder {
- param($folderUrl)
- $folder = $web.GetFolder($folderUrl)
- foreach($file in $folder.Files) {#Ensure destination directory
- $destinationfolder = $destination + "/" + $folder.Url
- if (!(Test - Path - path $destinationfolder)) {
- $dest = New - Item $destinationfolder - type directory
- }#Download file
- $binary = $file.OpenBinary()
- $stream = New - Object System.IO.FileStream($destinationfolder + "/" + $file.Name), Create
- $writer = New - Object System.IO.BinaryWriter($stream)
- $writer.write($binary)
- $writer.Close()
- }
- }#Download root files
- ProcessFolder($list.RootFolder.Url)#Download files in folders
- foreach($folder in $list.Folders) {
- ProcessFolder($folder.Url)
- }
using Server object model use below code
- static void Main(string[] args) {
- using(SPSite site = new SPSite("http://site name")) {
- using(SPWeb web = site.OpenWeb()) {
-
- SPList list = web.Lists["Shared Documents"];
- processFolder(list.RootFolder.Url);
- foreach(SPFolder folder in list.RootFolder.SubFolders) {
- processFolder(folder.Url);
- }
- }
- }
- }
- public static void processFolder(string folderURL) {
- string Destination = @
- "c:\\temp";
- using(SPSite site = new SPSite("http://yoursite")) {
- using(SPWeb web = site.OpenWeb()) {
- SPFolder folder = web.GetFolder(folderURL);
- foreach(SPFile file in folder.Files) {
- string destinationfolder = Destination + "/" + folder.Url;
- byte[] binary = file.OpenBinary();
- if (!Directory.Exists(destinationfolder)) {
- Directory.CreateDirectory(destinationfolder);
- }
- FileStream stream = new FileStream(destinationfolder + "/" + file.Name, FileMode.Create);
- BinaryWriter writer = new BinaryWriter(stream);
- writer.Write(binary);
- writer.Close();
- }
- }
- }
- }
using Client object model use below code
- static void Main(string[] args) {
- var site = new ClientContext("http://sitename/");
- var web = site.Web;
- site.Load(web);
- site.ExecuteQuery();
-
- List list = web.Lists.GetByTitle("Shared Documents");
- site.Load(list);
- site.ExecuteQuery();
- site.Load(list.RootFolder);
- site.ExecuteQuery();
- site.Load(list.RootFolder.Folders);
- site.ExecuteQuery();
- processFolderClientobj(list.RootFolder.ServerRelativeUrl);
- foreach(Folder folder in list.RootFolder.Folders) {
- processFolderClientobj(folder.ServerRelativeUrl);
- }
- }
- public static void processFolderClientobj(string folderURL) {
- string Destination = @
- "c:\\temp";
- var site = new ClientContext("http://sitename/");
- var web = site.Web;
- site.Load(web);
- site.ExecuteQuery();
- Folder folder = web.GetFolderByServerRelativeUrl(folderURL);
- site.Load(folder);
- site.ExecuteQuery();
- site.Load(folder.Files);
- site.ExecuteQuery();
- foreach(Microsoft.SharePoint.Client.File file in folder.Files) {
- string destinationfolder = Destination + "/" + folder.ServerRelativeUrl;
- Stream fs = Microsoft.SharePoint.Client.File.OpenBinaryDirect(site, file.ServerRelativeUrl).Stream;
- byte[] binary = ReadFully(fs);
- if (!Directory.Exists(destinationfolder)) {
- Directory.CreateDirectory(destinationfolder);
- }
- FileStream stream = new FileStream(destinationfolder + "/" + file.Name, FileMode.Create);
- BinaryWriter writer = new BinaryWriter(stream);
- writer.Write(binary);
- writer.Close();
- }
- }
- public static byte[] ReadFully(Stream input) {
- byte[] buffer = new byte[16 * 1024];
- using(MemoryStream ms = new MemoryStream()) {
- int read;
- while ((read = input.Read(buffer, 0, buffer.Length)) > 0) {
- ms.Write(buffer, 0, read);
- }
- return ms.ToArray();
- }
- }