In this tutorial, I will tell you how to Download all Files from Document Library in SharePoint 2013. Now, write the following code snippet in PowerShell:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
######################## Start Variables ######################## ######################## Varun's Script###################### $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) } |
Now, write the following code in Server object model:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
static void Main(string[] args) { using(SPSite site = new SPSite("http://site name")) { using(SPWeb web = site.OpenWeb()) { // Library name - Shared Documents 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(); } } } } |
In the Client object model use the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
static void Main(string[] args) { var site = new ClientContext("http://sitename/"); var web = site.Web; site.Load(web); site.ExecuteQuery(); // Library name - Shared Documents 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(); } } |
I hope it helps!
SharePoint 2013 Hosting Recommendation
HostForLIFE.eu’s SharePoint 2013 Hosting solution offers a comprehensive feature set that is easy-to-use for new users, yet powerful enough for the most demanding web developer expert. Hosted SharePoint Foundation 2013 is the premiere web-based collaboration and productivity enhancement tool on the market today. With SharePoint 2013 Foundation, you can quickly access and manage documents and information anytime, anywhere though a Web browser in a secure and user friendly way. SharePoint hosting services start at only at €9.99/mo, allowing you to take advantage of the robust feature set for a small business price. HostForLIFE.eu offers a variety of hosted SharePoint Foundation 2013 plans as well as dedicated SharePoint 2013 Foundation options.