Thursday, February 5, 2009

How to Download all files from SharePoint Library?

Using following code we can download all the documents including Subfolders.

SPList list = web.Lists["Shared Documents"];
SPView view = list.Views["All Documents"];
SPQuery squery = new SPQuery(view);
squery.ViewAttributes = “Scope=\”Recursive\”";
SPListItemCollection items = list.GetItems(squery);

//Read all documents and write those files to Local Machine
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();
}

0 comments:

Post a Comment