PHP-EWS: Set multiple extended properties on calendar item

不想你离开。 提交于 2019-12-11 17:12:51

问题


Is it possible to set multiple custom properties on a calendar item using PHP EWS? I haven't been able to find any documentation on this except this example of retrieving extended properties. I was able to get it working for a single field, but I'm wondering if you can set multiple custom properties. The API seems to allude to that possibility.

For example, the following properties are defined in ExtendedPropertyType.php:

class EWSType_ExtendedPropertyType extends EWSType
{
    /**
     * ExtendedFieldURI property
     *
     * @var EWSType_PathToExtendedFieldType
     */
    public $ExtendedFieldURI;

    /**
     * Value property
     *
     * @var string
     */
    public $Value;

    /**
     * Values property
     *
     * @var EWSType_NonEmptyArrayOfPropertyValuesType
     */
    public $Values;
}

The $Values property appears to be an array, but I was never able to store anything there successfully. My workaround was to collapse an array of values into a JSON string and store it in the $Value property (see my answer below). That works, but it feels a little hackish. Is there a better way?


回答1:


Here's my workaround in the mean time (just the pertinent pieces). Store multiple values as a JSON string in the $Value property:

Set the property when saving the calendar item:

// define custom property
$extendedProperty = new EWSType_PathToExtendedFieldType();
$extendedProperty->PropertyName = 'MyCustomProperty';
$extendedProperty->PropertyType = EWSType_MapiPropertyTypeType::STRING;
$extendedProperty->DistinguishedPropertySetId = EWSType_DistinguishedPropertySetIdType::PUBLIC_STRINGS;
$request->Items->CalendarItem->ExtendedProperty = new EWSType_ExtendedPropertyType();
$request->Items->CalendarItem->ExtendedProperty->ExtendedFieldURI = $extendedProperty;

// store custom data as JSON string
$custom_data = array(
    'scheduled_by' => 'staff',
    'send_to' => $users_email
);
$request->Items->CalendarItem->ExtendedProperty->Value = json_encode($custom_data);

Retrieve the property when reading the calendar:

// initialize the request
$request = new EWSType_FindItemType();
$request->Traversal = EWSType_ItemQueryTraversalType::SHALLOW;
$request->ItemShape = new EWSType_ItemResponseShapeType();
$request->ItemShape->BaseShape = EWSType_DefaultShapeNamesType::ALL_PROPERTIES;
$request->ItemShape->AdditionalProperties = new EWSType_NonEmptyArrayOfPathsToElementType();

// get custom property
$extendedProperty = new EWSType_PathToExtendedFieldType();
$extendedProperty->PropertyName = 'MyCustomProperty';
$extendedProperty->PropertyType = EWSType_MapiPropertyTypeType::STRING;
$extendedProperty->DistinguishedPropertySetId = EWSType_DistinguishedPropertySetIdType::PUBLIC_STRINGS;
$request->ItemShape->AdditionalProperties->ExtendedFieldURI = array($extendedProperty);

Decode the JSON for each calendar item in the response:

// get JSON data from custom property
$custom_data = json_decode($item->ExtendedProperty->Value, true);



回答2:


$oProperty = new EWSType\ExtendedPropertyType();
$oProperty->ExtendedFieldURI = ExchangeConnector_Connection_Connector::getExtendedFieldUri();
$oProperty->Value = $this->_iCRMId;

if ( isset( $oItem->ExtendedProperty ) ) {
    if ( !is_array( $oItem->ExtendedProperty ) ) {
        $oItem->ExtendedProperty = [ $oItem->ExtendedProperty ];
    }
    $oItem->ExtendedProperty[] = $oProperty;
} else {
    $oItem->ExtendedProperty = $oProperty;
}

This works for me. print_r looks like this:

[ExtendedProperty] => Array
                            (
                                [0] => PhpEws\DataType\ExtendedPropertyType Object
                                    (
                                        [ExtendedFieldURI] => PhpEws\DataType\PathToExtendedFieldType Object
                                            (
                                                [DistinguishedPropertySetId] => 
                                                [PropertySetId] => 
                                                [PropertyTag] => 0x3A45
                                                [PropertyName] => 
                                                [PropertyId] => 
                                                [PropertyType] => String
                                            )

                                        [Value] => Herr
                                        [Values] => 
                                    )

                                [1] => PhpEws\DataType\ExtendedPropertyType Object
                                    (
                                        [ExtendedFieldURI] => PhpEws\DataType\PathToExtendedFieldType Object
                                            (
                                                [DistinguishedPropertySetId] => 
                                                [PropertySetId] => ef11e53c-f1b8-45bd-8d2a-db90c5498569
                                                [PropertyTag] => 
                                                [PropertyName] => crm_record_id
                                                [PropertyId] => 
                                                [PropertyType] => String
                                            )

                                        [Value] => 76
                                        [Values] => 
                                    )

                            )


来源:https://stackoverflow.com/questions/28550495/php-ews-set-multiple-extended-properties-on-calendar-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!