1
votes

I'm writing a program for importing contacts from an ERP system to Outlook. Different emails will receive different lists of contacts from ERP. The idea here is, in each email I have a public contact folder that can be accessed by a technical user. The technical user can write contacts into this folder. Here is the code for searching the folder:

protected FolderId findFolderId(String folderDisplayName, String userEmail) throws Exception {
    Mailbox userMailbox = new Mailbox(userEmail);
    FolderId contactRootFolder = new FolderId(WellKnownFolderName.Root, userMailbox);

    FolderId result = null;
    FolderView view = new FolderView(Integer.MAX_VALUE);
    view.setPropertySet(new PropertySet(BasePropertySet.IdOnly, FolderSchema.DisplayName));
    view.setTraversal(FolderTraversal.Deep);
    FindFoldersResults findFolderResults = this.service.findFolders(contactRootFolder, view);
    //find specific folder
    for (Folder f : findFolderResults) {
        if (folderDisplayName.equals(f.getDisplayName())) {
            result = f.getId();
        }
    }

    return result;
}

The service object is created as follows:

this.service = new ExchangeService();
    ExchangeCredentials credentials = new WebCredentials(userName, passWord);
    this.service.setCredentials(credentials);

    try {
        this.service.setUrl(new URI(URL));
    } catch (URISyntaxException e) {
        LOGGER.error(e);
    }

Where URL is the end point for the Exchange server (for Office 365 it is https://outlook.office365.com/EWS/Exchange.asmx).

The code works with Office 2010, I get the Id from that folder, connect to it and save the contacts. After the migration to Office 365, we can't find the public folder. It can just find a folder with the name "PeoplePublicData". (I don't even know that folder exists.)

1

1 Answers

1
votes

Throttling in Office365 means your code will only return the first 1000 folder in the Mailbox so if what your looking for isn't within that result set that would be one reason. I would suggest you get rid of

FolderView view = new FolderView(Integer.MAX_VALUE);

and change it to

FolderView view = new FolderView(1000);

and then page the results https://msdn.microsoft.com/en-us/library/office/dn592093(v=exchg.150).aspx which will allow you to get all the Folder in a Mailbox. Also unless you are looking for something in the Non_IPM_Subtree of the Mailbox start the search with MsgFolderRoot eg

FolderId contactRootFolder = new FolderId(WellKnownFolderName.MsgFolderRoot, userMailbox);

That will reduce the number of folders returned.

Also why don't you use a SearchFilter to search for the folder you are after eg https://msdn.microsoft.com/en-us/library/office/dd633627(v=exchg.80).aspx this would eliminate the need to page the results,