Get folders in same order as in Outlook via C# with Microsoft.Office.Interop.Outlook

Mario Ableidinger 1 Reputation point
2021-03-23T15:24:58.107+00:00

How do I get the folders in the same order as they appear in Outlook?
I am using this code to get the folders:

static void Main(string[] args)  
{  
  
    var outlookApp = new Microsoft.Office.Interop.Outlook.Application();  
    var ns = outlookApp.GetNamespace("MAPI");  
    var folder = ns.Folders[2];  
    Console.WriteLine($"+++ {folder.Name} +++");  
    foreach (Microsoft.Office.Interop.Outlook.Folder subFolder in folder.Folders)  
    {  
        Console.WriteLine(subFolder.Name);  
    }  
}  

Then I get this list of results:

80903-5.jpg

However, in Outlook they are displayed in this order:

80904-6.jpg

Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-03-24T03:08:46+00:00

    In Microsoft.Office.Interop.Outlook, these folders are sorted by StoreID by default.

    But in the Outlook application, it seems that we can only manually adjust the order or sort by the first letter of the name, and when sorting by the first letter, Inbox, Drafts, Sent Items, Deleted Items do not participate in the sorting, and are always in the top four.

    I tried to find a way to get the folder sorting in the current outlook application in the code, but unfortunately it seems impossible to do it. I will continue to investigate and if there are new discoveries, I will tell you immediately.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.