using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.IO;
using Microsoft.SharePoint.Administration;
namespace ExportDocuments
{
class Exportdoc
{
private static void Main()
{
string strDownloadPath = @"C:\MyDownloadFolder\";//physical location
string url = "http://Site_Url";
using (SPSite oSiteCollection = new SPSite(url))
{
SPWebCollection collWebsites = oSiteCollection.AllWebs;
Console.WriteLine("Websites Count: {0}", collWebsites.Count);
Console.WriteLine(" ");
string rootwebtitle = oSiteCollection.RootWeb.Title;
foreach (SPWeb oWebsite in collWebsites)
{
Console.WriteLine("Site : " + oWebsite.Title);
//Path to download the files
string websiteurl = oWebsite.Url;
string framedUrl = websiteurl.Replace(oSiteCollection.Url, "");
string path = strDownloadPath + "\\" + rootwebtitle + framedUrl.Replace('/', '\\');
Directory.CreateDirectory(path);
DownloadDocs(oWebsite.Url, path);
oWebsite.Dispose();
Console.WriteLine("--------------------");
}
}
Console.WriteLine("Please click enter to exit");
Console.ReadLine();
}
private static void DownloadDocs(string siteURL, string sitepath)
{
//List of OOTB document libraries - These doc libraries will not be downloaded
List<string> ootbLib = new List<string>();
ootbLib.Add("Converted Forms");
ootbLib.Add("Form Templates");
ootbLib.Add("Images");
ootbLib.Add("List Template Gallery");
ootbLib.Add("Master Page Gallery");
ootbLib.Add("Pages");
ootbLib.Add("Reporting Templates");
ootbLib.Add("Site Collection Documents");
ootbLib.Add("Site Collection Images");
ootbLib.Add("Site Template Gallery");
ootbLib.Add("Style Library");
ootbLib.Add("Web Part Gallery");
using (SPSite oSite = new SPSite(siteURL))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
foreach (SPList list in oWeb.Lists)
{
string actpath = sitepath + "\\" + list.Title+ "\\" ;
if (list.BaseType == SPBaseType.DocumentLibrary)
{
if (!ootbLib.Exists(delegate(string p) { return p.Trim() == list.Title; }))
{
Console.WriteLine("Downloading from Library ::" + list.Title);
Directory.CreateDirectory(actpath);
TraverseAllListItems(list,actpath);
}
}
}
}
}
}
private static void TraverseAllListItems(SPList list,string path)
{
SPQuery qry = new SPQuery();
qry.ViewAttributes = "Scope=\"Recursive\"";
try
{
SPListItemCollection ic = list.GetItems(qry);
foreach (SPListItem subitem in ic)
{
string actpath = path + "\\" + subitem.Url;
string itemurl = subitem.Url;
int index = itemurl.IndexOf("/");
string subpath = itemurl.Substring(index + 1);
string finalpath = path + subpath;
finalpath = finalpath.Replace(subitem.Name, "");
finalpath = finalpath.Replace("/", "\\");
Console.WriteLine(finalpath);
Directory.CreateDirectory(finalpath);
byte[] binFile = subitem.File.OpenBinary();
System.IO.FileStream fstream = System.IO.File.Create(finalpath + "\\" + subitem.Name);
fstream.Write(binFile, 0, binFile.Length);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
Happy coding :)