Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating a Contacts Physical Address with PHP EWS

i am busy writing some code that will update the physical address on a Microsoft exchange server via PHPEWS;

but for the life of me i cant quite figure out how to update the physical address, i can update everything else but this.

here is my code.

// Update Physical Address.
$field = new EWSType_SetItemFieldType();
$field->IndexedFieldURI->FieldURI = 'contacts:PhysicalAddress:Street';
$field->IndexedFieldURI->FieldIndex = EWSType_PhysicalAddressKeyType::HOME;

$field->Contact = new EWSType_ContactItemType();
$field->Contact->PhysicalAddress = new EWSType_PhysicalAddressDictionaryType();

$address = new EWSType_PhysicalAddressDictionaryEntryType();
$address->Key = EWSType_PhysicalAddressKeyType::HOME;
$address->_ = $street;

$field->Contact->PhysicalAddresses->Entry = $address;
$change->Updates->SetItemField[] = $field;

i keep getting the following error

Array ( [0] => stdClass Object ( [MessageText] => An object within a change description must contain one and only one property to modify. [ResponseCode] => ErrorIncorrectUpdatePropertyCount [DescriptiveLinkKey] => 0 [ResponseClass] => Error [Items] => stdClass Object ( ) ) ) 

hope someone can help

like image 216
Philip csaplar Avatar asked Nov 19 '12 14:11

Philip csaplar


1 Answers

after many hours of trial and error i finally worked it out myself.

here is the code,

// Update Physical Address.
$field = new EWSType_SetItemFieldType(); 
$field->IndexedFieldURI->FieldURI = 'contacts:PhysicalAddress:Street';
$field->IndexedFieldURI->FieldIndex = EWSType_PhysicalAddressKeyType::HOME;

$field->Contact = new EWSType_ContactItemType();
$field->Contact->PhysicalAddresses = new EWSType_PhysicalAddressDictionaryType();
$address = new EWSType_PhysicalAddressDictionaryEntryType();
$address->Key = EWSType_PhysicalAddressKeyType::HOME;

$field->Contact->PhysicalAddresses->Entry = $address;
$field->Contact->PhysicalAddresses->Entry->Street = $street;

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

Basically you set your FieldURI and Field Index (one must remember that when updating you can only update one item at a time) you will see the FieldURI is set to "contacts:PhysicalAddress:Street" this is because you can only update one item at a time.

next we create the Contact tag then the PhysicalAddresses tag then the Entry tag and give it the Key of Home and finally we set the Street tag.

the actual XML it create will look something like this.

<t:SetItemField>
<t:IndexedFieldURI FieldURI="contacts:PhysicalAddress:CountryOrRegion" FieldIndex="Business" />
<t:Contact>
<t:PhysicalAddresses>
<t:Entry Key="Business">
<t:CountryOrRegion>
</t:CountryOrRegion>
</t:Entry>
</t:PhysicalAddresses>
</t:Contact>
</t:SetItemField>

And thats it, it then updates the Street address.All you need to do now is put the code in a loop , use a switch to see which part of the address you want to update and bobs your uncle.

oh well hope this helps someone.

like image 64
Philip csaplar Avatar answered Sep 24 '22 13:09

Philip csaplar