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.

Download all files from SharePoint Library
Tagged on:             

2 thoughts on “Download all files from SharePoint Library

  • October 22, 2009 at 5:55 PM
    Permalink

    Very handy information.
    Thanks

    Reply
  • June 22, 2018 at 5:01 PM
    Permalink

    Hello,

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

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *