If you are a programmer building an Outlook solution that may require the use of Search Folders, you may not actually want the user to know that you are using Search Folders. Sure, you can work with search results in code and you don’t have to show the results. But using a Search Folder actually gives you a lot more power than you may realize (and an upcoming post will detail more about this). However, Outlook is really limited for programmatically working with Search Folders. You can’t create them, nor easily work with their results (unless you actually start with Application.AdvancedSearch to get a Search object and call Search.Save to create a Search Folder and keep the Search object alive) or access a Search Folder’s scope or search criteria.
When I say you “can’t”, I mean of course with the Outlook Object Model. This is where Redemption comes in. And you cannot possibly create professional Outlook solutions without using Redemption in some capacity at some point. If you’re averse to using third-party solutions, then be my guest: go find a C++ expert who is also an Extended MAPI expert. And good luck with that. Otherwise, Redemption has two implementations for integrating with Outlook Search Folders: the RDOSearchFolder object and the RDOSearch object.
If you want to create a Search Folder that you can use in the Outlook UI, then the RDOSearch object is what you need. You can choose from the many methods that RDOSearches.Add provides (AddCategorized, AddConversations, AddCustom, etc.) for creating templated-based Search Folders. But these Search Folders are created in the SearchRoot container and thus visible in the Outlook UI (and cannot be hidden).
So how do we create a hidden Search Folder? This is where RDOSearchFolder comes in. These objects can be used to create a hidden implementation of RDOSearch, but does require a bit more effort to build the search criteria and to set the search containers. However, unlike RDOSearch, you can create an RDOSearchFolder object anywhere in the store. All you need is an RDOFolders collection (from any folder, thus via RDOFolder.Folders) and you can call RDOFolders.AddSearchFolder. You just need to make sure that you create this folder in a hierarchy that’s not visible in the Outlook UI. And this location is essentially any folder outside of the IPM_SUBTREE (and NOT in RDOStore.SearchRootFolder of course, which is the parent of all visible Outlook Search Folders) which is the container for everything you see under you Mailbox or above your default folders on the same level. So when you create it anywhere else it is effectively hidden – which means you can’t see it in the Search Folders hierarchy, nor anywhere in the Folders module or Navigation Pane (or any folder pickers, for that matter).
But if it is hidden, how can you still display it? You have to use code, but it does work! I was actually surprised that Outlook didn’t puke somehow, crash or show the entire gazillion character EntryID value for the folder in the Title bar. All you need to do to show this supposedly hidden folder is to use your choice of either setting Explorer.CurrentFolder or calling Folder.Display. You of course have to get the Folder object first, so you’ll have to do some “folder walking” by name or better yet use NameSpace.GetFolderFromID with a saved EntryID value for the Search Folder.
So here’s some sample VBA code to create one of these RDOSearchFolder babies (sorry, you’ll have to convert it to VB.NET or C# for use in an add-in, but the concepts are the same). Much of this is verbatim to Dmitry’s sample code in the RDO reference, modified for this post:
On Error Resume Next
'DON'T FORGET TO SET A REFERENCE TO THE REDEMPTION OBJECT LIBRARY!
Dim Session As Redemption.RDOSession
Dim DefaultStore As Redemption.RDOStore
Dim SearchRootFolder As Redemption.RDOFolder
Dim NewSearchFolder As Redemption.RDOSearchFolder
Dim Restriction As Object
Dim objHiddenFolder As Outlook.Folder
Dim objNS As Outlook.NameSpace
Dim objExpl As Outlook.Explorer
RES_CONTENT = 3
FL_SUBSTRING = 1
FL_IGNORECASE = &H10000
PR_SUBJECT = &H37001E
Set Session = New Redemption.RDOSession
Set DefaultStore = Session.Stores.DefaultStore
<span style="color: #ff0000;"><strong> Set SearchRootFolder = DefaultStore.RootFolder</strong></span>
<span style="color: #ff0000;"><strong> 'create new search folder</strong></span>
<span style="color: #ff0000;"><strong> Set NewSearchFolder = SearchRootFolder.folders.AddSearchFolder("Test Redemption Search Folder")</strong></span>
'set the restriction to search for message with the word "test" in the subject line
Set Restriction = NewSearchFolder.SearchCriteria.SetKind(RES_CONTENT)
Restriction.ulFuzzyLevel = FL_SUBSTRING Or FL_IGNORECASE
Restriction.ulPropTag = PR_SUBJECT
Restriction.lpProp = "test"
'specify that the search should be performed in the Inbox and Sent Items folders
'specify the search flags
NewSearchFolder.IsRecursiveSearch = False
NewSearchFolder.IsForegroundSearch = False
'we are all set: set the search parameters and let the fun begin!
'Now you don't see me...
Set objNS = Application.GetNamespace("MAPI")
Set objHiddenFolder = objNS.GetFolderFromID(NewSearchFolder.EntryID)
Set objExpl = ActiveExplorer
<span style="color: #000000;"> 'Now you do!</span>
<span style="color: #ff0000;"><strong> Set objExpl.CurrentFolder = objHiddenFolder</strong></span>
'Output the EntryID to the Immediate Window in case you want to quickly access the same folder again via GetFolderFromID
Run the code above, and you’ll see the search folder get created and navigated to in a blink of an eye. See, it works! But remember that you always have to use code to show this folder. Note also that the Customize This Search Folder button will still be visible in the Actions Group in the Folder tab when a hidden search folder is navigated to, but the Customize dialog will not be able to show any search criteria or folder choices defined in the Search Folder. So one major drawback is that if you still wish your solution to open up customization of this Search Folder to your users, then you’ll have to build that entire UI from scratch (or at least as much of that functionality that you can stomach recreating).
BTW, you can still use MFCMAPI or OutlookSpy to see hidden folders. MFCMAPI is good at showing folder hierarchies just like Outlook, so you can visualize where the IPM_SUBTREE and the SearchRoot (Finder) is, as well as where exactly our Test Redemption Search Folder that we created above is located: