Exchange Web服务搜索自定义文件夹名称

时间:2016-09-08 19:24:13

标签: java exchangewebservices ewsjavaapi

我需要找到文件夹名称“Archived”,它将位于Parent级别,而不是SUBFOLDER,如果文件夹“Archived”是一个子文件夹,我不希望它返回结果集。这就是我编码的内容,但这也会返回子文件夹。

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) {
            // look for the desired folder name
            System.out.println(folder.getDisplayName());
            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

1 个答案:

答案 0 :(得分:2)

  
    

我需要找到文件夹名称&#34;已存档&#34;这将是在父级别

  

如果是这种情况,您不应该使用深度遍历,因为这将返回层次结构中的所有文件夹,而不仅仅是顶层的文件夹。我还建议您使用SearchFilter,这样它只返回您想要的文件夹,这将简化您的代码,例如

        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);
        }