5

I have been able to successfully retrieve the unread emails from an Exchange 2010 inbox using php-ews API. However after I have fetched the emails, I want to set the IsRead property of the email to true, so that these messages do not appear the next time I fetch emails.

Anyone done this before ?

EDIT :

This is how I am trying to set the IsRead flag :

$message_id = ''; //id of message
$change_key = ''; //change key   
$response = $ews->GetItem($request);
        //print_r($response);exit;
        if( $response->ResponseMessages->GetItemResponseMessage->ResponseCode == 'NoError' &&
            $response->ResponseMessages->GetItemResponseMessage->ResponseClass == 'Success' ) {

            $a = array();
            $message = $response->ResponseMessages->GetItemResponseMessage->Items->Message;

            $a['message_body'] = $message->Body->_;
            $a['sender'] = $message->From->Mailbox->EmailAddress;
            $a['subject'] = $message->ConversationTopic;

            $data[] = $a;
            //process the message data.

            $messageType = new EWSType_MessageType();
            $messageType->IsRead = true;

            $path = new EWSType_PathToUnindexedFieldType();
            $path->FieldURI = 'message:IsRead';

            $setField = new EWSType_SetItemFieldType();
            $setField->Message = $messageType;
            $setField->FieldURI = $path;


            $u = new EWSType_ItemChangeType();
            $u->Updates = new EWSType_NonEmptyArrayOfItemChangeDescriptionsType();
            $u->Updates->SetItemField = $setField;
            $u->ItemId = new EWSType_ItemIdType();
            $u->ItemId->Id = $message_id;
            $u->ItemId->ChangeKey = $change_key;

            $updatedItems = new EWSType_NonEmptyArrayOfItemChangesType();
            $updatedItems->ItemChange = $u;

            $updateMessenger = new EWSType_UpdateItemType();
            $updateMessenger->ItemChanges = $updatedItems;
            $updateMessenger->MessageDisposition = 'SaveOnly';
            $updateMessenger->ConflictResolution = 'AutoResolve';

            try {
                $update_response = $ews->UpdateItem($updateMessenger);
            }catch (Exception $e){
                echo $e->getMessage();
            }
        }

When I run the file I get the following error :

An internal server error occurred. The operation failed.

After debugging for some time, I have concluded that the error happens at the curl_exec function in NTLMSoapClient.php file.

I dont know where to go on from here. Please help.

Lin
  • 273
  • 4
  • 21
  • Hi, I found out the problem by myself. Turns out it was becasue of the Exchange version that I specified when I create the ews object. The updateItem function executes but throws this error : An object within a change description must contain one and only one property to modify. Any ideas ? – Lin Sep 11 '12 at 14:13

3 Answers3

2

I've faced a similar issue when updating a calendar event and setting the IsAllDayEvent flag. This is the code that worked for me:

$ews = new ExchangeWebServices(...);

$request = new EWSType_UpdateItemType();
$request->ConflictResolution = 'AlwaysOverwrite';
$request->ItemChanges = array();

$change = new EWSType_ItemChangeType();
$change->ItemId = new EWSType_ItemIdType();
$change->ItemId->Id = $id;
$change->ItemId->ChangeKey = $changeKey;

$field = new EWSType_SetItemFieldType();
$field->FieldURI = new EWSType_PathToUnindexedFieldType();
$field->FieldURI->FieldURI = "calendar:IsAllDayEvent";
$field->CalendarItem = new EWSType_CalendarItemType();
$field->CalendarItem->IsAllDayEvent = true;

$change->Updates->SetItemField[] = $field;

$request->ItemChanges[] = $change;
$response = $ews->UpdateItem($request);

The biggest difference I see here is that you do $u->Updates->SetItemField = $setField;, whereas my code uses $u->Updates->SetItemField[] = $setField;.

I hope this helps.

Edit: You might have already seen this, but I based my code on the one from the php-ews wiki.

0

I tried everything including PathToExtendedFieldType and it doesn't work at the end code below worked for me

$ews = new ExchangeWebServices('red', 'red', 'red',ExchangeWebServices::VERSION_2007_SP1);

$request = new EWSType_UpdateItemType();

$request->SendMeetingInvitationsOrCancellations = 'SendToNone';
$request->MessageDisposition = 'SaveOnly';
$request->ConflictResolution = 'AlwaysOverwrite';
$request->ItemChanges = array();

// Build out item change request.
$change = new EWSType_ItemChangeType();
$change->ItemId = new EWSType_ItemIdType();
$change->ItemId->Id = $contact_id;
$change->ItemId->ChangeKey = $contact_change_key;
#$change->Updates = new EWSType_NonEmptyArrayOfItemChangeDescriptionsType();

#$change->Updates->SetItemField = array();

// Build the set item field object and set the item on it.
$field = new EWSType_SetItemFieldType();
$field->FieldURI = new EWSType_PathToUnindexedFieldType();
$field->FieldURI->FieldURI = "message:IsRead";
$field->Message = new EWSType_MessageType();
$field->Message->IsRead = true;


$change->Updates->SetItemField[] = $field;
$request->ItemChanges[] = $change;

$response = $ews->UpdateItem($request);
var_dump($response);
Pang
  • 9,564
  • 146
  • 81
  • 122
redknight
  • 39
  • 6
-1

Well, i dont know how it is in php, but in C# there is another field, that must be set: IsReadSpecified = true.

email.IsRead = true;
email.IsReadSpecified = true;
IStar
  • 184
  • 1
  • 15