Download all files from SharePoint Library

Now, we are going to see how we can download all documents(incluing sub folders) from SharePoint Library.

First of all,we have get all documents from SharePoint Library. The following code help us to get all the documents including Subfolders.
//Get the Document Library and its view
SPList list = web.Lists[“Test Document”];
SPView view = list.Views[“All Documents”];

//Query all documents in Library including files under subfolders
SPQuery squery = new SPQuery(view);
squery.ViewAttributes = “Scope=”Recursive””;
SPListItemCollection items = list.GetItems(squery);

Now we have to write a code for downloaing documents,
//Read all documents and write those files to Local System
foreach (SPListItem item in items)
{
byte[] binfile = item.File.OpenBinary();
FileStream fstream = new FileStream(“F:\” + item.File.Name, FileMode.Create, FileAccess.ReadWrite);
fstream.Write(binfile, 0, binfile.Length);
fstream.Close();
}

By using the above code snippets, we can download all document from SharePoint Library.

Shantha Kumar
Shantha Kumar
Articles: 280

24,849 Comments

  1. Hello,

    Is this some JavaScript? How ones can use this code in SharePoint?
    I canĀ“t find how to make it works unfortunately.

Comments are closed.