Search code examples
sharepointsharepoint-2010sharepoint-2007splist

How to copy document library documents to local drive programmatically in Sharepoint 2007


How do I copy document library documents to local drive programmatically in Sharepoint 2007?


Solution

  • I'm not at my computer but something like this should do the trick.

    using (SPSite site = new SPSite("http://sitecol"))
    {
       SPWeb web = site.RootWeb;
       SPFolder docLib = web.GetFolder("DocLibName");
    
       foreach (SPFile f in docLib.Files)
       {
          // Use RWEP when accessing local file system
          SPSecurity.RunWithElevatedPrivileges(delegate(){
          {
             System.IO.File.WriteAllBytes(@"C:\Location\" + f.Name, f.OpenBinary());
          }
       }
    }
    

    Basically grab your site collection, then grab your spweb object, then grab your document library as a SPFolder. Then do a foreach loop through all the files in that folder, saving the byte array to your local filesystem.

    Hope this helps!