Garethp / php-ews

PHP Exchange Web Services
BSD 3-Clause "New" or "Revised" License
112 stars 46 forks source link

FindItem and Restrictions with multiple IsEqualTo #42

Closed jwaschkau closed 4 years ago

jwaschkau commented 8 years ago

Hi,

when running this request I get the error that the 'Or' element is not valid. When using only one 'IsEqualTo' everything is working fine. But i want to filter by multiple custom ids. Any idas?

EXTENDED_PROPERTY_ID = array(
    'PropertySetId' => 'c11ff723g-af43-4555-9952-8f4248a11c3e',
    'PropertyName' => 'customproperty',
    'PropertyType' => 'Integer',
);
 $request = [
            'ItemShape' => array(
                'BaseShape' => 'IdOnly',
                'AdditionalProperties' => [
                    'ExtendedFieldURI' => ExchangeContactService::EXTENDED_PROPERTY_ID,
            ]),
            'Traversal' => 'Shallow',
            'ParentFolderIds' => array(
                'FolderId' => $folderId->toXmlObject()
            ),
            'Restriction' => array(
                    array('Exists' => array(
                        'ExtendedFieldURI' => EXTENDED_PROPERTY_ID,
                    )),
                    'Or' => array(
                        array('And' => array(
                            'IsEqualTo' => array(
                                'ExtendedFieldURI' => EXTENDED_PROPERTY_ID,
                                'FieldURIOrConstant' => array(
                                    'Constant' => array(
                                        'Value' => 7)
                                ),
                            ),
                        )),
                        array('And' => array(
                            'IsEqualTo' => array(
                                'ExtendedFieldURI' => EXTENDED_PROPERTY_ID,
                                'FieldURIOrConstant' => array(
                                    'Constant' => array(
                                        'Value' => 8)
                                ),
                            ),
                        )),
                    )
                ),
            ]
$request = array_replace_recursive($request, $options);
$request = API\Type::buildFromArray($request);

return $this->api->getClient()->FindItem($request);
Garethp commented 8 years ago

I'll take a look in to this, but I'm not familiar with Restrictions. Unfortunately I'm moving countries next week, and I won't have my main dev laptop, so if I can find something before the weekend I'll be able to help, but otherwise a resolution to this might be a couple of weeks out

Garethp commented 8 years ago

Maybe it's because I don't have the right extended properties, but I fail whenever I try to use your custom ID's in Restrictions. Here's what I tried

<?php

require_once "vendor/autoload.php";

use garethp\ews\API\Type;
use garethp\ews\Mail\MailAPI as API;

$api = API::withUsernameAndPassword(
    'https://outlook.office365.com',
    'email',
    'password'
);

$EXTENDED_PROPERTY_ID = [
    'PropertySetId' => 'c11ff723g-af43-4555-9952-8f4248a11c3e',
    'PropertyName' => 'customproperty',
    'PropertyType' => 'Integer'
];

$request = [
    'Traversal' => 'Shallow',
    'ItemShape' => [
        'BaseShape' => 'AllProperties',
    ],
    'ParentFolderIds' => [
        'FolderId' => $api->getFolderId()->toXmlObject()
    ],
    'Restriction' => [
        [
            'Exists' => [
                'ExtendedFieldURI' => $EXTENDED_PROPERTY_ID
            ]
        ],

        'Or' => [
            'IsEqualTo' => [
                [
                    'FieldURI' => ['FieldURI' => 'message:IsRead'],
                    'FieldURIOrConstant' => ['Constant' => ['Value' => 'false']]
                ],
                [
                    'FieldURI' => ['FieldURI' => 'message:IsRead'],
                    'FieldURIOrConstant' => ['Constant' => ['Value' => 'true']]
                ]
            ],
        ]
    ]
];

$request2 = [
    'ItemShape' => [
        'BaseShape' => 'AllProperties',
    ],
    'Traversal' => 'Shallow',
    'ParentFolderIds' => array(
        'FolderId' => $api->getFolderId()->toXmlObject()
    ),
    'Restriction' => array(
        array(
            'Exists' => array(
                'ExtendedFieldURI' => $EXTENDED_PROPERTY_ID,
            )
        ),
        'Or' => array(
            'And' => [
                [
                    'IsEqualTo' => [
                        [
                            'FieldURI' => ['FieldURI' => 'message:IsRead'],
                            'FieldURIOrConstant' => ['Constant' => ['Value' => 'true']]
                        ],
                        [
                            'FieldURI' => ['FieldURI' => 'message:IsRead'],
                            'FieldURIOrConstant' => ['Constant' => ['Value' => 'true']]
                        ]
                    ],
                ],
            ],
            'IsEqualTo' => array(
                [
                    'FieldURI' => ['FieldURI' => 'message:IsRead'],
                    'FieldURIOrConstant' => ['Constant' => ['Value' => 'true']]
                ],
            )
        )
    ),
];

$request3 = [
    'Traversal' => 'Shallow',
    'ItemShape' => [
        'BaseShape' => 'AllProperties',
    ],
    'ParentFolderIds' => [
        'FolderId' => $api->getFolderId()->toXmlObject()
    ],
    'Restriction' => [
        [
            'Exists' => [
                'ExtendedFieldURI' => $EXTENDED_PROPERTY_ID
            ]
        ],

        'IsEqualTo' => [
            [
                'ExtendedFieldURI' => $EXTENDED_PROPERTY_ID,
                'FieldURIOrConstant' => ['Constant' => ['Value' => 7]]
            ]
        ],
    ]
];

$request = Type::buildFromArray($request3);

$items = $api->getClient()->FindItem($request);

exit();

While I didn't check that the restrictions were returning the correct results, $request and $request2 ran with no errors, while $request3 throws a Uncaught SoapFault exception: [a:ErrorInvalidRequest] The request is invalid. in /development/php-ews/src/API/NTLMSoapClient.php:127 Exception

jwaschkau commented 8 years ago

Hey i see. The example has the wrong property id which is no valid hex value. I am using

EXTENDED_PROPERTY_ID = array(
    'PropertySetId' => 'c11ff723g-af43-4555-9952-8f4248a11c3e',
    'PropertyName' => 'customproperty',
    'PropertyType' => 'Integer',
);

Sorry for that.

Also in item shape the extended properties must be added like this, so that it is searchable. Are custom properties included in AllProperties?

'ItemShape' => array(
   'BaseShape' => 'IdOnly',
    'AdditionalProperties' => [
           'ExtendedFieldURI' => ExchangeContactService::EXTENDED_PROPERTY_ID,
       ]
),

I am adding extended properties to a contact like this:

$soap = array_merge_recursive(
    [
        'AdditionalProperties' => [
            'ExtendedFieldURI' => ExchangeContactService::EXTENDED_PROPERTY_ID,
        ],
        'ExtendedProperty' => [
            'ExtendedFieldURI' => ExchangeContactService::EXTENDED_PROPERTY_ID,
            'Value' => 7,
        ],
    ],
    $soap
);

Where $soap is the array with contact data:

$soap = array (
   'Surname' => 'Doe',
   ...
)

I will do some testing as soon as i get some time. It has no high priority because when searching only one custom property everything is working.

umangpadh commented 5 years ago

How can I get emails between 2 dates (start date and end date) using getMailItems() method?

Garethp commented 4 years ago

I'm closing this issue as I've reworked the "simple" Restrictions API to allow for multiple conditions as seen in the issue linked above