0

I'm using PHP-EWS (https://github.com/jamesiarmes/php-ews) inside a cakePHP application. The goal is to read E-Mails from a "Public Folder" from the exchange server.

The Problem is I can only read the first "Dimension" of public folders and can't find a way to get the subdirectorys.

The folder I have to read from is 4 levels deep.

 $this->connect();

// start building the find folder request 
$request = new FindFolderType();
$request->Traversal = FolderQueryTraversalType::SHALLOW;
$request->FolderShape = new FolderResponseShapeType();
$request->FolderShape->BaseShape = DefaultShapeNamesType::ALL_PROPERTIES;

// configure the view
$request->IndexedPageFolderView = new IndexedPageViewType();
$request->IndexedPageFolderView->BasePoint = 'Beginning';
$request->IndexedPageFolderView->Offset = 0;

// set the starting folder
$request->ParentFolderIds = new NonEmptyArrayOfBaseFolderIdsType();
$request->ParentFolderIds->DistinguishedFolderId = new    DistinguishedFolderIdType();
$request->ParentFolderIds->DistinguishedFolderId->Id = DistinguishedFolderIdNameType::PUBLIC_FOLDERS_ROOT;

// request 
$response = $this->ews->FindFolder($request);

If I change "Traversal" to DEEP I get the error .

DEEP TRAVERSAL queries are not allowed for public folders.

I also tried to change

$request->IndexedPageFolderView->BasePoint

To things like "end" "second", it didn't change anything so I could not figure out what it does and how to use it.

I can't get the subdirectory Folder id (for changing the start point) either because it gets never selected.

Thank you for your help.

Elydasian
  • 2,016
  • 5
  • 23
  • 41
M41DZ3N
  • 326
  • 1
  • 4
  • 19

1 Answers1

1

Very good question. Unfortunately the library that you've chosen is out of date and unmaintained. I personally would suggest you use my more up to date one, garethp/php-ews.

I don't know if this is the best solution, but what I would suggest is to get the first level folder, then the second and so on. So if you know the directory structure of your folders, and it looked something like this

- Folder 1
    - Subfolder 1
        - Subfolder 2
            - Subfolder 3 (Target)
- Folder 2
- Folder 3

Then first you'd get Folder 1, which would be a child of DistinguishedFolderIdNameType::PUBLIC_FOLDERS_ROOT. Then you would get Subfolder 1 which would be a child of Folder 1, then get Subfolder 2, then Subfolder 3. I can't advise you on how you would manage that with the library you're currently using, but with mine it would look something like

$api = MailAPI::withUsernameAndPassword($host, $username, $password);
$folder1 = $api->getFolderByDisplayName('Folder1', Enumeration\DistinguishedFolderIdNameType::PUBLICFOLDERSROOT);
$subFolder1 = $api->getFolderByDisplayName('Subfolder1', $folder1->getFolderId());
$subFolder2 = $api->getFolderByDisplayName('Subfolder2', $subfolder1->getFolderId());
$subFolder3 = $api->getFolderByDisplayName('Subfolder3', $subfolder2->getFolderId());
$api->setFolderId($subFolder3->getFolderId());

Obviously that's a lot of calls, so if you're using that folder ID often, I would save the FolderID to a database for quicker retrieval later

Gareth Parker
  • 5,012
  • 2
  • 18
  • 42
  • can i use your forke without composer ? – M41DZ3N Jun 14 '16 at 11:34
  • If you write an autoloader, sure. But I would recommend just using composer – Gareth Parker Jun 14 '16 at 12:49
  • Hey Gareth, i managed to get the Folder with your API but if i search for items i run into a sopa error (object has no constant property) $api->getMailItems($subFolder->getFolderId(), array('Restriction' => array('contains' => 'array('FieldURI' => $searchstring)))); – M41DZ3N Jun 15 '16 at 07:55
  • What's the value of searchstring? – Gareth Parker Jun 15 '16 at 08:43
  • Something like "New" or "project abc" as example – M41DZ3N Jun 15 '16 at 08:57
  • Yeah, you need an actual fieldUri, those would not be FieldURI's – Gareth Parker Jun 15 '16 at 09:39
  • can you give me an example code on how the options array should look? i can't figure out what the function expects from me – M41DZ3N Jun 15 '16 at 10:34
  • You need to know how to use Exchange Web Services. If you know what the XML is meant to look like, then you try to replicate that with the array structure. In your example, you needed to pass in an actual Field URI, such as `message:subject`. Here's the MSDN docs on search filters with EWS https://msdn.microsoft.com/en-us/library/office/dn579422(v=exchg.150).aspx – Gareth Parker Jun 15 '16 at 10:59
  • Im getting really close thanks for the input i use now $options = array('Restriction' => array('contains' => array( 'subject' => 'stringvalue' ))); but there is still an error some where i getting a SOAP error object has no 'Constant' property – M41DZ3N Jun 15 '16 at 11:47
  • This is going out of the scope of the original stack overflow question, and comments are not a good place to solve issues like this. Can you log an issue against my Github? – Gareth Parker Jun 15 '16 at 12:35