view raw
Lucky Lucky - 8 months ago 53
Java Question

Exchange Web Service Search for Custom folder name

I need to find the folder name "Archived" which will be at the Parent level, NOT A SUBFOLDER, if folder "Archived" 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));

try {

FindFoldersResults findFolderResults = service.findFolders(new FolderId(WellKnownFolderName.MsgFolderRoot, userMailbox) ,view);
//find specific folder
for (Folder folder : findFolderResults) {
// 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();
//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


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 then 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 eg

        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,"");

        FindFoldersResults folderSearchResults = service.FindFolders(SearchRoot, fsFolderSearch, fvFolderView);
        if (folderSearchResults.Folders.Count == 1)