Search code examples
c#outlookvstooutlook-2010

How to retrieve a list of all custom folders in Outlook containing only MailItems?


I am attempting to retrieve a list of all custom folders created by the user such that I can do a Global Advanced Search. I only want custom folders with MailItems objects contained within them, so I want to exclude several folders from the list (e.g. RSS Feeds, Quick Step Settings, Suggested Contacts) which come standard in a outlook store.

First, this question is very similiar to questions asked below, but the selected answers did not address custom folders--just all folders.

Does anybody know how to retireve a list of custom folders that contain only mailitem objects? I have created a fairly large if block to filter out unwanted folders, but I need a more generic approach since this will be deployed on user computers and who-knows-what people do with their outlook folders?

if (
    // remove specific search folders from cope
    subfolder.Name == "Quick Step Settings" || subfolder.Name == "News Feed" || subfolder.Name == "Conversation History" || subfolder.Name == "Conversation Action Settings" ||

    // remove unwanted default folders from returning expiring items
    subfolder.FolderPath == Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar).FolderPath ||
    subfolder.FolderPath == Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderConflicts).FolderPath ||
    subfolder.FolderPath == Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts).FolderPath ||
    subfolder.FolderPath == Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDeletedItems).FolderPath ||
    subfolder.FolderPath == Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderJournal).FolderPath ||
    subfolder.FolderPath == Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderRssFeeds).FolderPath ||
    subfolder.FolderPath == Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSuggestedContacts).FolderPath ||
    subfolder.FolderPath == Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSyncIssues).FolderPath ||
    subfolder.FolderPath == Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderServerFailures).FolderPath ||
    subfolder.FolderPath == Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderJunk).FolderPath ||
    subfolder.FolderPath == Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderLocalFailures).FolderPath ||
    subfolder.FolderPath == Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks).FolderPath ||

    // eliminate certain custom folders with unwanted default types
    subfolder.DefaultItemType == Outlook.OlItemType.olContactItem || subfolder.DefaultItemType == Outlook.OlItemType.olJournalItem
    || subfolder.DefaultItemType == Outlook.OlItemType.olAppointmentItem || subfolder.DefaultItemType == Outlook.OlItemType.olDistributionListItem
    || subfolder.DefaultItemType == Outlook.OlItemType.olDistributionListItem
    )
{
    addFolder = false;
}

Solution

  • You will need to recursively loop through all folders starting with Namespace.Folders. For each folder, check the DefaultItemType property. If you want to exclude default mail folders (such as Inbox and Sent Items), compare the folder's entry with with the entry id of the default folders retrieved from GetDefaultFolder.