none
Enumerating through all outlook folders recursively RRS feed

  • Question

  • Hi all and TIA

    i would like to be able to enumerate the folders in an outlook account using recursion. I can go through the folders ok as i have seen a few pages describing how to do it.

    My problem is i want to add them to a TreeView in C# as i recurse them. i have had them at various positions with a few tests, all show as child of the first folder, show as child of correct folder but also show as their own entry in parent branch(?). From what i have tested i recurse easily enough it is when it comes to adding the node at the correct level and then effectively moving back up a level when required.

    Im sure this is a very easy task but it is eluding me at the moment. This is kind of the route i am going at the moment, but i have played around with it so much tomight im not sure it will make sense.

    public void Enumerate_Folders(Outlook.MAPIFolder root)
            {
                int folder_count = 0;
                TreeNode node1 = new TreeNode();
                
                    Outlook.Folders folders = root.Folders;
                    
    
                    node1 = layer1.Nodes.Add(root.Name);
    
                    folder_count = folders.Count;
    
    
                    if (folder_count > 0)
                    {
    
    
                        foreach (Outlook.Folder Folder in folders)
                        {
                            node1.Nodes.Add(Folder.Name);
                            Enumerate_Folders(Folder);
                        }
                        //if(Folder.DefaultItemType == Outlook.OlItemType.olContactItem)
                        //Stores_list.Add(Folder.FolderPath);
                    }
                    else
                    {
                        node1.Nodes.Add(folders[0].Name);
                        return;
                    }
                   
                }

    any gems of wisdom, greatly appreciated

    :)

    • Moved by Lisa Zhu Thursday, February 28, 2013 6:21 AM outlook dev related
    Tuesday, February 26, 2013 11:37 PM

Answers

  • Hello jonydexter,

    try something like this.

     private void LoadTree()
            {
                Microsoft.Office.Interop.Outlook.Application app = null;
                Microsoft.Office.Interop.Outlook._NameSpace ns = null;
                Microsoft.Office.Interop.Outlook.PostItem item = null;
                Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
                Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;

                try
                {
                    app = new Microsoft.Office.Interop.Outlook.Application();
                    ns = app.GetNamespace("MAPI");
                    ns.Logon(null, null, false, false);

                    TreeNode root = new TreeNode();
                    root.Text = "MAPI";

                    Microsoft.Office.Interop.Outlook.Folders folders = ns.Folders;

                    foreach (Microsoft.Office.Interop.Outlook.Folder folder in folders)
                    {
                        TreeNode newnode = new TreeNode();
                        newnode.Text = folder.Name;

                        LoadSubFolder(ref newnode, folder.Folders);

                        //foreach (Microsoft.Office.Interop.Outlook.Folder subfolder in folder.Folders)
                        //{
                        //    TreeNode newsub = new TreeNode();
                        //    newsub.Text = subfolder.Name;
                        //    newnode.Nodes.Add(newsub);
                        //}

                        root.Nodes.Add(newnode);
                    }

                    root.Expand();
                    treeView1.Nodes.Add(root);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    ns = null;
                    app = null;
                    inboxFolder = null;
                }
            }

            private void LoadSubFolder(ref TreeNode node, Microsoft.Office.Interop.Outlook.Folders folders)
            {
                try
                {
                    foreach (Microsoft.Office.Interop.Outlook.Folder subfolder in folders)
                    {
                        TreeNode newNode = new TreeNode();
                        newNode.Text = subfolder.Name;
                        LoadSubFolder(ref newNode, subfolder.Folders);

                        node.Nodes.Add(newNode);
                    }
                }
                catch (Exception ex) { }
            }

    i hope that this works for you.

    best regards.


    VItor Teixeira

    • Marked as answer by jonnydexter Wednesday, February 27, 2013 7:38 PM
    Wednesday, February 27, 2013 4:03 PM

All replies

  • I think your problem is that your method only takes one parameter.  You need to supply it with the Outlook folder to start drawing and the TreeNode to start drawing at.  Your basic idea seems good but each time you call yourself you are adding the TreeNodes to layer1, you need to add them to the current TreeNode.

    The intial call to Enumerate_Folders would pass the root of the Outlook folder tree and the root node of your TreeView.  The method would add the subfolders to the tree and then call itself with the current subfolder and the current tree node.

    Does that make sense?


    Paul Linton

    Wednesday, February 27, 2013 1:00 AM
  • Hi Paul

    thanks for the reply. It makes perfect sense, i was trying this prior to the above but thought i was over complicating things so went back to basics.

    I couldnt figure out the best way to change an index within the resursion, so that the nodes were added in the correct place.

    • Proposed as answer by Vítor Teixeira Wednesday, February 27, 2013 4:01 PM
    • Unproposed as answer by Vítor Teixeira Wednesday, February 27, 2013 4:01 PM
    Wednesday, February 27, 2013 9:27 AM
  • Hello jonydexter,

    try something like this.

     private void LoadTree()
            {
                Microsoft.Office.Interop.Outlook.Application app = null;
                Microsoft.Office.Interop.Outlook._NameSpace ns = null;
                Microsoft.Office.Interop.Outlook.PostItem item = null;
                Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
                Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;

                try
                {
                    app = new Microsoft.Office.Interop.Outlook.Application();
                    ns = app.GetNamespace("MAPI");
                    ns.Logon(null, null, false, false);

                    TreeNode root = new TreeNode();
                    root.Text = "MAPI";

                    Microsoft.Office.Interop.Outlook.Folders folders = ns.Folders;

                    foreach (Microsoft.Office.Interop.Outlook.Folder folder in folders)
                    {
                        TreeNode newnode = new TreeNode();
                        newnode.Text = folder.Name;

                        LoadSubFolder(ref newnode, folder.Folders);

                        //foreach (Microsoft.Office.Interop.Outlook.Folder subfolder in folder.Folders)
                        //{
                        //    TreeNode newsub = new TreeNode();
                        //    newsub.Text = subfolder.Name;
                        //    newnode.Nodes.Add(newsub);
                        //}

                        root.Nodes.Add(newnode);
                    }

                    root.Expand();
                    treeView1.Nodes.Add(root);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    ns = null;
                    app = null;
                    inboxFolder = null;
                }
            }

            private void LoadSubFolder(ref TreeNode node, Microsoft.Office.Interop.Outlook.Folders folders)
            {
                try
                {
                    foreach (Microsoft.Office.Interop.Outlook.Folder subfolder in folders)
                    {
                        TreeNode newNode = new TreeNode();
                        newNode.Text = subfolder.Name;
                        LoadSubFolder(ref newNode, subfolder.Folders);

                        node.Nodes.Add(newNode);
                    }
                }
                catch (Exception ex) { }
            }

    i hope that this works for you.

    best regards.


    VItor Teixeira

    • Marked as answer by jonnydexter Wednesday, February 27, 2013 7:38 PM
    Wednesday, February 27, 2013 4:03 PM
  • Hi thanks for the code. I have tried it and it works fine. Appreciated. Just goignt o disect it now and get my head around what its doing.

    i have noticed that if i remove the "ref" keyword from the treenode it still 'appears' to work fine, what is the relevance of ref in this case (i understand ref, but why does it no need to be there)

    thanks again


    • Edited by jonnydexter Wednesday, February 27, 2013 7:46 PM
    Wednesday, February 27, 2013 7:38 PM