• Download all files from SharePoint Library

    by  • April 11, 2009 • SharePoint • 1 Comment

    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.

    One Response to Download all files from SharePoint Library

    1. Mahesh
      October 22, 2009 at 5:55 PM

      Very handy information.
      Thanks

    Leave a Reply

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