none
How to populate a tree view with the list of all folders and subfolders using C#

    Question

  • I want to populate a treeview with the list of all folders and subfolders in outlook.

    I have a Windows form having a tree view on it. I want to  populate it on load event with all folders and subfolders in outlook.I am new to the Add-in development.So please tell me how could i do that?

    Thursday, September 20, 2012 4:35 AM

Answers

  • Because you are calling GetDefaultFolder(Inbox)?

    You can start with the Inbox.Parent folder (which will give you the top folder of the default store) or you can start with Namespace.Folders (that collection has top folders of all stores in the profile).


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    • Marked as answer by NirajZ Monday, September 24, 2012 11:51 AM
    Friday, September 21, 2012 1:59 PM

All replies

  • i have created an add in for outlook. It includes one windows  having  a tree view control. i want to display all foders and sub folders into that tree view control. I have done with displaying one folder named "Inbox" but not getting all other folders and subfolders . This is my source code: Please update the source code as per need and let me know

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Microsoft.Office.Interop.Outlook;
    using System.Collections;

    namespace OutlookAddIn2
    {
        public partial class SaveToFolder : Form
        {
            
            Microsoft.Office.Interop.Outlook._NameSpace ns = null;
            Microsoft.Office.Interop.Outlook.Application app = null;
           
            public SaveToFolder()
            {
                InitializeComponent();
            }

            private void button2_Click(object sender, EventArgs e)
            {

            }

            private void SaveToFolder_Load(object sender, EventArgs e)
            {
                Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
                app = new Microsoft.Office.Interop.Outlook.Application();
                ns = app.GetNamespace("MAPI");         
                inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
                //inboxFolder = ns.GetSharedDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderDeletedItems);
                GetFolders(inboxFolder);
            }

           
            public void GetFolders(MAPIFolder currentFolder)
            {
                TreeNodeCollection nodes = null;
                if (currentFolder == null)
                {
                    // If current folder is null, we’ll be adding the root folders to
                    // the TreeView itself
                    nodes = treeView1.Nodes;
                }
                else
                {
                    // otherwise, find the node that represent the MAPIFolder we’re about
                    // to expand
                     treeView1.Visible = true;
                      //treeView1.Nodes.Clear() ;
                      //treeView1.Nodes.Add(currentFolder.Name);

                      TreeNode parentNode = treeView1.Nodes.Add(currentFolder.Name);
                    if (parentNode == null)
                        throw new ArgumentException("currentFolder");
                    nodes = parentNode.Nodes;
                }

                // If this folder was already expanded, there’s nothing left for us to do
                if (currentFolder != null && folderExpanded[currentFolder] != null)
                    return;
                else if (currentFolder != null)
                {
                    // Otherwise, clear the children nodes of the node we’re about to expand
                    // This is done since we might have placed a Dummy node there - see later
                    if (nodes.Count > 0)
                        nodes.Clear();
                    // Mark folder as expanded
                    folderExpanded[currentFolder] = true;
                }

                // Retrieve the root/sub folders
                Folders folders = (currentFolder == null ? nameSpace.Folders : currentFolder.Folders);
                foreach (MAPIFolder folder in folders)
                {
                    // If this folder already has a node, we’re done with it
                    if (folderToNode[folder] != null)
                        continue;
                    // Generate a node for the current subfolder
                    TreeNode newNode = GenerateNode(folder);
                    // Add folder to hashes and to it’s parent node
                    nodeToFolder[newNode] = folder;
                    folderToNode[folder] = newNode;
                    nodes.Add(newNode);
                }
            }

            private TreeNode GenerateNode(MAPIFolder folder)
            {
                // Create a new node with the correct name
                TreeNode newNode = new TreeNode(folder.Name);
                // Check if and how the number of items should be displayed
                switch (folder.ShowItemCount)
                {
                    case OlShowItemCount.olNoItemCount:
                        // Don’t display the number of items
                        break;
                    case OlShowItemCount.olShowTotalItemCount:
                        // Display the total number of items
                        if (folder.Items.Count > 0)
                            newNode.Text += " (" + folder.Items.Count + ")";
                        break;
                    case OlShowItemCount.olShowUnreadItemCount:
                        // Display only the number of unread items
                        if (folder.UnReadItemCount > 0)
                            newNode.Text += " (" + folder.UnReadItemCount + ")";
                        break;
                }

                // If folder has any subfolders, create a dummy node beneath it. This is
                // used so that the node will have the nice plus (+) sign next to it
                // so the user will know there are subfolders. This Dummy node will be
                // removed once the folder is actually expanded
                //if (folder.Folders.Count > 0)
                ////newNode.Nodes.Add(new TreeNode(DummyNode));
                //{
                //    Microsoft.Office.Interop.Outlook.MAPIFolder subfolder = null;
                //    subfolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
                //    GenerateNode(subfolder);
                //}

                return newNode;
            }

            private const string DummyNode = "{1194595F-CAF7-474b-8972-CF143F097243}";
            private Hashtable nodeToFolder = new Hashtable();
            private Hashtable folderToNode = new Hashtable();
            private Hashtable folderExpanded = new Hashtable();
            Outlook.NameSpace nameSpace;


        }
    }
            
        

    Thursday, September 20, 2012 11:35 AM
  • Can you step through your code and check which line behaves unexpectedly?

    In general, people on this forum will help you with particular questions ("how do I?" or "why X happens when I do Y"), but don't expect that someone will write code for you.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Thursday, September 20, 2012 5:10 PM
  • How could i populate all folders & subfolders  from outlook 2010 to a tree view control in a outlook add-in  Program using C#?
    Friday, September 21, 2012 4:12 AM
  • By recursively processing all folders starting witt the Namespace.Folders collection.

    What are you trying to do and why? Have you looked into the Namespace.PickFolder method?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Friday, September 21, 2012 5:00 AM
  • I am new  to the development world but curious about new things. I haven't searched for that method.

    Could you please redirect me to specific reference so that i can get it.

    Friday, September 21, 2012 5:12 AM
  • See http://msdn.microsoft.com/en-us/library/office/ff869969.aspx


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Friday, September 21, 2012 5:52 AM
  • i AM GETTING ONLY FOLDERS UNDER INBOX,

    HOW COULD I GET FOLDERS OTHER THAN INBOX AND ITS SUB FOLDERS.?????

    Friday, September 21, 2012 11:10 AM
  • Because you are calling GetDefaultFolder(Inbox)?

    You can start with the Inbox.Parent folder (which will give you the top folder of the default store) or you can start with Namespace.Folders (that collection has top folders of all stores in the profile).


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    • Marked as answer by NirajZ Monday, September 24, 2012 11:51 AM
    Friday, September 21, 2012 1:59 PM
  • The WinForms TreeView Control offers a powerful Data Binding support, drag and drop items capability.

    http://www.kettic.com/winforms_ui/treeview.shtml

    Thursday, September 05, 2013 9:19 AM