1

I need to find the folder named "Archived", which will be at the parent level, NOT A SUBFOLDER. If the "Archived" folder is a subfolder, I don't want that to return in the result set. This is what I have coded, but this returns the sub folders as well.

HashMap<String, FolderId> folderIdList = new HashMap<String, FolderId>();

FolderId readFromId = null; // Folder to read emails from - Inbox
FolderId moveToId = null;   // Folder to move emails to after processing - Archived

FolderView view = new FolderView(Integer.MAX_VALUE);

view.setPropertySet(new PropertySet(BasePropertySet.IdOnly));
view.getPropertySet().add(FolderSchema.DisplayName);
view.setTraversal(FolderTraversal.Deep);

try {
    FindFoldersResults findFolderResults = service.findFolders(
        new FolderId(WellKnownFolderName.MsgFolderRoot, userMailbox), view);

    // find specific folder
    for (Folder folder : findFolderResults) {
        System.out.println(folder.getDisplayName());

        // look for the desired folder name
        if (folder.getDisplayName().equalsIgnoreCase("Inbox")) {
            // get the id
            readFromId = folder.getId();
        }
            // add the folder id to map
            folderIdList.put(UtilConstants.READ_FROM, folder.getId());

        } else if (folder.getDisplayName().equalsIgnoreCase("Archived")) {
            // get the id
            moveToId = folder.getId();
        } else {
            // add the folder id to map
            folderIdList.put(UtilConstants.MOVE_TO, folder.getId());
        }
    }
} catch (Exception e) {
    log.error("Error while getting the folder id's for folders " +
        readFrom + ", " + moveTo + "" + e.getMessage());
    throw e;
}

return folderIdList;

enter image description here

Image transcription:

Folder Name - Archived
Folder Id - AAMkADJlZDRmYmRiLWUzMmEtNGMzOC1hMWQ4LWUyMmI4M2ZkNzVhMgAuAAAAAAAoHfw6vz/jR685bS6+CozVAQBBPVBF5/AsS73+rEIOWaRMAAARBHT4AAA=
Child Folder Count - 1
Parent Folder Id - AQMkADJlZDRmYmRiLWUzMmEtNGMzOC1hMWQ4LWUyMgBiODNmZDc1YTIALgAAAygd/Dq/P+NHrzltLr4KjNUBAEE9UEXn8CxLv6sQg5ZpEwAAAIBDAAAAA==
------------------------------------------------------------------------------------------
Folder Name - Inbox
Folder Id - AQMkADJlZDRmYmRiLWUzMmEtNGMzOC1hMWQ4LWUyMgBiODNmZDc1YTIALgAAAygd/Dq/P+NHrzltLr4KjNUBAEE9UEXn8CxLv6sQg5ZpEwAAAIBDAAAAA==
Child Folder Count - 0
Parent Folder Id - AQMkADJlZDRmYmRiLWUzMmEtNGMzOC1hMWQ4LWUyMgBiODNmZDc1YTIALgAAAygd/Dq/P+NHrzltLr4KjNUBAEE9UEXn8CxLv6sQg5ZpEwAAAIBCAAAAA==
------------------------------------------------------------------------------------------
Folder Name - Calendar
Folder Id - AQMkADJlZDRmYmRiLWUzMmEtNGMzOC1hMWQ4LWUyMgBiODNmZDc1YTIALgAAAygd/Dq/P+NHrzltLr4KjNUBAEE9UEXn8CxLv6sQg5ZpEwAAAIBDQAAAA==
Child Folder Count - 0
Parent Folder Id - AQMkADJlZDRmYmRiLWUzMmEtNGMzOC1hMWQ4LWUyMgBiODNmZDc1YTIALgAAAygd/Dq/P+NHrzltLr4KjNUBAEE9UEXn8CxLv6sQg5ZpEwAAAIBDAAAAA==
------------------------------------------------------------------------------------------
Folder Name - Contacts
Folder Id - AQMkADJlZDRmYmRiLWUzMmEtNGMzOC1hMWQ4LWUyMgBiODNmZDc1YTIALgAAAygd/Dq/P+NHrzltLr4KjNUBAEE9UEXn8CxLv6sQg5ZpEwAAAIBDgAAAA==
Child Folder Count - 7
Parent Folder Id - AQMkADJlZDRmYmRiLWUzMmEtNGMzOC1hMWQ4LWUyMgBiODNmZDc1YTIALgAAAygd/Dq/P+NHrzltLr4KjNUBAEE9UEXn8CxLv6sQg5ZpEwAAAIBDAAAAA==
CarenRose
  • 1,266
  • 1
  • 12
  • 24
Lucky
  • 783
  • 2
  • 10
  • 28

1 Answers1

3

I need to find the folder name "Archived" which will be at the Parent level

If that's the case, you shouldn't be using a deep traversal as that will return all folders in the hierarchy, rather than just those at the top level. I would also suggest you use a SearchFilter so it just returns the folder you want, which will simplify your code, for example:

String folderSearchName = "Archived";

FolderView fvFolderView = new FolderView(1);
fvFolderView.Traversal = FolderTraversal.Shallow;

SearchFilter fsFolderSearch = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, folderSearchName);

FolderId SearchRoot = new FolderId(WellKnownFolderName.MsgFolderRoot, "user@domain.com");

FindFoldersResults folderSearchResults = service.FindFolders(SearchRoot, fsFolderSearch, fvFolderView);
if (folderSearchResults.Folders.Count == 1) 
{
    Console.WriteLine(folderSearchResults.Folders[0].DisplayName);
}
Abra
  • 19,142
  • 7
  • 29
  • 41
Glen Scales
  • 20,495
  • 1
  • 20
  • 23
  • Thanks Glen, I tried your solution, I'm getting 1 result for "Archived", even tho I have 3 "Archived" folders, 2 were sub folders. Now is there a property which I can check on the one folder that i'm getting back in the result is a Parent folder only. – Lucky Sep 09 '16 at 21:38
  • I edited my question, Added a picture of the Folder id, Child folder Count and Parent folder id, What is this Parent folder Id which is common for all Folders, I was thinking If I can compare this Parent folder id with Archived folder Parent folder id, If Archived is a sub folder, Parent folder id won't match. Or is there another property I can check. Thanks. – Lucky Sep 09 '16 at 21:49
  • If the Folder you want to get is at the Root level then just using a Shallow traversal is the best method like i posted. You can't have two folder using the same name at the same folder level eg you can have two folders name archived at the MsgfolderRoot level. here is no common parent FolderId because a mailbox folder are a hierarchy think of Treeview. The parent folder only matter if the folder you want to get is a subfolder of something else eg Inbox or user created folder but that doesn't sound like the case for you. – Glen Scales Sep 11 '16 at 23:20