0

I am trying to download everything (documents, lists, folders) of a web and its sub-webs and its sub-webs (if exists) and so on, I can do it for a single web but its not working for subwebs in it, code is given below,

   private void downloadList(SPObjectData objectData)
    {
        using (SPWeb currentWeb = objectData.Web)
        {
            foreach (SPList list in currentWeb.Lists)
            {
                    foreach (SPFolder oFolder in list.Folders)
                    {
                        if (oFolder != null)
                        {
                            foreach (SPFile file in oFolder.files)
                            {
                                if (CreateDirectoryStructure(tbDirectory.Text, file.Url))
                                {
                                    var filepath = System.IO.Path.Combine(tbDirectory.Text, file.Url);
                                    byte[] binFile = file.OpenBinary();
                                    System.IO.FileStream fstream = System.IO.File.Create(filepath);
                                    fstream.Write(binFile, 0, binFile.Length);
                                    fstream.Close();
                                }
                            }
                        }
                }
            }
        }
    }
Muhammad Raja
  • 1,970
  • 3
  • 28
  • 46
  • Cheers, i don't think so. What is not working? Do you get an exception? It feels you want us to debug your code... – rene Dec 28 '11 at 17:04
  • I think you didn;t read my question properly :), anyway I explain it again, Problem is I can get it working for 1 single web, but I am struggling to find a way to download an entire site, or a web with sub webs and so on. Cheers – Muhammad Raja Dec 28 '11 at 17:31

1 Answers1

0

That is because you want to do it recursively for the subwebs

foreach(SPWeb oWeb in currentWeb.Webs){

downloadList(oWeb); //use same logic you used above to get all the stuff from the sub web

}

So, it would be like this for your recursive method:

//notice I overloaded
private void downloadList(SPWeb oWeb){
//get subwebs of subwebs
foreach(SPWeb oWeb in currentWeb.Webs){

   downloadList(oWeb); 

}
foreach (SPList list in oWeb.Lists)
            {
                    foreach (SPFolder oFolder in list.Folders)
                    {
                        if (oFolder != null)
                        {
                            foreach (SPFile file in oFolder.files)
                            {
                                if (CreateDirectoryStructure(tbDirectory.Text, file.Url))
                                {
                                    var filepath = System.IO.Path.Combine(tbDirectory.Text, file.Url);
                                    byte[] binFile = file.OpenBinary();
                                    System.IO.FileStream fstream = System.IO.File.Create(filepath);
                                    fstream.Write(binFile, 0, binFile.Length);
                                    fstream.Close();
                                }
                            }
                        }
                }
            }
        }
}
Algorhythm
  • 570
  • 4
  • 17
  • there's some confusion in your code, please review it :) e.g. current web doesn't exists in the context plus i am using SPObjectData not SPWeb as argument , if i pass SPWeb as argument there will be problem with memory management and it wont gonna dispose it properly. Cheers – Muhammad Raja Dec 29 '11 at 08:54