3

Code:

$api = MailAPI::withUsernameAndPassword($server, $username, $password);
$folder1 = $api->getFolderByDisplayName('PubFolder', DistinguishedFolderIdNameType::PUBLICFOLDERSROOT);
$subFolder1 = $api->getFolderByDisplayName($data['mailfolder'], $folder1->getFolderId());
$api->setFolderId($subFolder1->getFolderId());

$mail = $api->getMailItems($data['id']);

Error: Exception 'Error' with message 'Call to a member function toXmlObject() on string'

Description: I'd like to get a single Mail item via the id, however since i need to call it from a different page and load the content via JS i am unable to send the MailID object which i can get by using $singleMail->getItemId() inside a foreach. So i have to use $singleMail->getItemId()->getId() which yields the ID as a string, however when trying to get the mail via ID I get the above error.

So, how should i proceed? Requesting all Mails and looping till i find the ID again is not an option.

Am I using getMailItems() wrongly? If so please correct me.

Can i Create the correct ID-Object somehow?

Or is there an alternative way to query a Single Mail via the ID-String? Optionally I would be looking for the manuel building of the Query.

Shaeldon
  • 873
  • 4
  • 18
  • 28

1 Answers1

2

getMailItems does not take an email id, but a folder id and it requires it to be an object ($folderId) for the first parameter and you're giving a string. If you go and check the MailAPI::getMailItems source, you will understand exactly why you get that error. At line 91 we see:

'FolderId' => $folderId->toXmlObject()

there your code tries to call a member function from a string. I searched a bit and I think you can filter the mails by id with getMailItems by using the optional parameter $options.

First, try to set the ItemId to have the string value of mail id like this:

$mail = $api->getMailItems($subFolder1->getFolderId(), [
    'ItemIds' => [
        'ItemId' => $data['id'] // Or (new API\Type\ItemIdType($data['id']))
    ]
]);

If that won't work, then try to search again for the mail by using this code:

$mail = $api->getItem(new API\Type\ItemIdType($data['id']));

I don't have an EWS to test this, but I think that's the way you're going to solve the problem.

Christos Lytras
  • 36,310
  • 4
  • 80
  • 113
  • @Shaeldon did you try change the `$data['id']` to `new API\Type\ItemIdType($data['id'])` for the first method? Are you sure the second method does not return a single item also? `$mail = $api->getItem(new API\Type\ItemIdType($data['id']));` – Christos Lytras Aug 02 '17 at 13:51
  • To your comment: yes i tried both. I experimented with your answer a bit and changed it to: $newId = new API\Type\ItemIdType($data['id']); $mail = $api->getItem($newId); however i recieve 'Cannot use object of type garethp\ews\API\Type\MessageType as array' – Shaeldon Aug 02 '17 at 13:51
  • The `ItemIdType` has a `toArray()` method. Try to use that one like this `$mail = $api->getItem($newId->toArray());` – Christos Lytras Aug 02 '17 at 13:54
  • forget it, i am an idiot. your code works fine, i had some other debug shit there that was before the die(); – Shaeldon Aug 02 '17 at 13:55
  • thanks for the help, i'll award the bounty in ~18 hours when its possible – Shaeldon Aug 02 '17 at 13:57
  • You're welcome. EWS has a bit of a strange way that manages `Items` and `IDs`, but if you get used to it, you'll be able to resolve that kind of issues more easily. I'm glad that you got this solved. – Christos Lytras Aug 02 '17 at 14:00