0

I'm using https://github.com/Garethp/php-ews/ library to access to my public contact folder on Exchange server.

This is how i create a contact.

$api = API::withUsernameAndPassword($server, $user, $pass);
$folder = $api->getFolderByDisplayName('Public', Enumeration\DistinguishedFolderIdNameType::PUBLICFOLDERSROOT);
$contattiTotali = $api->getFolderByDisplayName('Contacts', $folder->getFolderId());
$id=$contattiTotali->getFolderId()->getId();
$api->setFolderId($contattiTotali->getFolderId());

$api->createContacts(array(
    'GivenName' => 'Homer',
    'Surname' => 'Simpson',
    'EmailAddresses' => array(
        'Entry' => array('Key' => Enumeration\EmailAddressKeyType::EMAIL_ADDRESS_1, '_value' => 'h.simpson@gmail.com')
    ),
    //Creating multiple entries
    'PhoneNumbers' => array(
        'Entry' => array(
            array('Key' => Enumeration\PhoneNumberKeyType::HOME_PHONE, '_value' => '000'),
            array('Key' => Enumeration\PhoneNumberKeyType::BUSINESS_PHONE, '_value' => '111'),
        )
    ),
    'PhysicalAddresses' => array(
        'Entry' => array(
            'Key' => Enumeration\PhysicalAddressKeyType::HOME,
            'street' => '123 Street',
            'city' => '123 City',
            'state' => '123 State',
            'countryOrRegion' => '123 Country',
            'postalCode' => '12345',
        )
    ),
));

The code, actually, works fine, but if i execute it several times, it duplicates the contact.

Is there a way to check if the contact (email address is good enough) already exists before creating a new one?

Biro
  • 199
  • 3
  • 16

1 Answers1

0

The easiest way to telling if a contact already exists with a particular email address is to use the ResolveName operation eg

    $request = new EWSType_ResolveNamesType();

    $request->UnresolvedEntry = "address@domain.com";
    $request->ReturnFullContactData = true;
    $return = $ews->ResolveNames($request);

    if ($return->ResponseMessages->ResolveNamesResponseMessage->ResponseCode == "NoError") {
        return $return->ResponseMessages->ResolveNamesResponseMessage->ResolutionSet->Resolution->Mailbox->EmailAddress;
    }
Glen Scales
  • 20,495
  • 1
  • 20
  • 23
  • Thankyou for your answer but EWSType_ResolveNamesType() seems to be not bundled with @Garethp php-ews library, but i found it in Yii2, but it's not working. this is the error thrown `Fatal error: Uncaught Exception: Call to undefined method Exchange\Ews::ResolveNames() in /Volumes/Biro-HD/Sites/ews/vendor/segpacto/yii2-ews/Ews.php:709 Stack trace: #0 [internal function]: Exchange\Ews->exceptionHandler(Object(Error)) #1 {main} thrown in /Volumes/Biro-HD/Sites/ews/vendor/segpacto/yii2-ews/Ews.php on line 709 ` – Biro Jun 28 '18 at 10:33