Search code examples
c#.netsharepointsharepoint-2007

Difference between SPFile and FolderItem (Logical Error in code)


 private void bFolder_Click(object sender, EventArgs e)
    {
        TreeNode currentNode = trvContent.SelectedNode;
        SPObjectData objectData = (SPObjectData)currentNode.Tag;
        using (SPWeb TopLevelWeb = objectData.Web)
        {
            dwnEachWeb(TopLevelWeb);
        }
    }

    private void dwnEachWeb(SPWeb TopLevelWeb)
    {
        if (TopLevelWeb.Webs.Count == 0)
        {
            dwnEachList(TopLevelWeb);
        }
        foreach (SPWeb ChildWeb in TopLevelWeb.Webs)
        {
            dwnEachWeb(ChildWeb);
            ChildWeb.Dispose();
        }
    }

    private void dwnEachList(SPWeb oWeb)
    {
        if (oWeb.Lists.Count != 0)
        {
            foreach (SPList oList in oWeb.Lists)
            {
                if (oList.ItemCount != 0 && oList.Title != "Master Page Gallery")
                {
                    dwnEachFolder(oList.RootFolder);
                }
            }
        }
    }

    private void dwnEachFolder(SPFolder oFolder)
    {
        if (oFolder.SubFolders.Count == 0)
        {
            dwnEachFile(oFolder);
        }
        foreach(SPFolder SubFolder in oFolder.SubFolders)
        {
            dwnEachFolder(SubFolder);
        }
    }

    private void dwnEachFile(SPFolder oFolder)
    {
        if (oFolder.Files.Count != 0)
        {
            foreach(SPFile ofile in oFolder.Files)
            {
                if (CreateDirectoryStructure(tbDirectory.Text, ofile.Url))
                {
                    var filepath = System.IO.Path.Combine(tbDirectory.Text, ofile.Url);
                    byte[] binFile = ofile.OpenBinary();
                    System.IO.FileStream fstream = System.IO.File.Create(filepath);
                    fstream.Write(binFile, 0, binFile.Length);
                    fstream.Close();
                }
            }
        }
    }

    //creating directory        
    private bool CreateDirectoryStructure(string baseFolder, string filepath)
    {
        if (!Directory.Exists(baseFolder)) return false;

        var paths = filepath.Split('/');

        for (var i = 0; i < paths.Length - 1; i++)
        {
            baseFolder = System.IO.Path.Combine(baseFolder, paths[i]);
            Directory.CreateDirectory(baseFolder);
        }
        return true;
    }

Code has a logical problem , tried to debug, but when it comes to code line

foreach(SPFile ofile in oFolder.Files)

in method dwnEachFile, it simply comes out of method without stating any problem and not creating any directory at all. however if I hover ova SPFile it says,

a file in sharepoint website that can be a web part page, an item in a document library or a file in a folder.


Solution

  • The Problem with code is I am looking for each file in a folder and its sub folder of a web, but I Was suppose to Look into each list and its sub list of a web and then each document library folder in each of the list.

    This MSDN link explains it all

    MSDN link