How can I find "People's Contacts" folders via Outlook's object model?
- by Dennis Palmer
I have some code that locates all the contact folders that a user has access to by iterating through the Application.Session.Stores collection.
This works for the user's contacts and also all the public contacts folders. It also finds all the contacts folders in additional mailbox accounts that the user has added via the Tools - Account Settings... menu command.
However, this requires the user to have full access to the other person's account. When a user only has access to another person's contacts, then that person's contacts show up under the "People's Contacts" group in the Contacts view. How do I find those contact folders that don't show up under Session.Stores?
In order to see the other user's contacts folder without adding access to their full mailbox, click File - Open - Other User's Folder... from the Outlook menu. In the dialog box, enter the other user's name and select Contacts from the Folder type drop down list.
Here's the code (minus the error checking and logging) I'm using to find a list of all the user's Outlook contact folders. I know this can (and maybe should) be done using early binding to the Outlook.Application type, but that doesn't affect the results. EnumerateFolders is recursive so that it searches all sub folders.
Dim folderList = New Dictionary(Of String, String)
Dim outlookApp = CreateObject(Class:="Outlook.Application")
For Each store As Object In outlookApp.Session.Stores
EnumerateFolders(folderList, store.GetRootFolder)
Next
Private Sub EnumerateFolders(ByRef folderList As Dictionary(Of String, String), ByVal folder As Object)
Try
If folder.DefaultItemType = 2 Then
folderList.Add(folder.EntryID, folder.FolderPath.Substring(2))
End If
For Each subFolder As Object In folder.Folders
EnumerateFolders(folderList, subFolder)
Next
Catch ex As Exception
End Try
End Sub