Search code examples
phpcalendarphp-ews

PHP-EWS: Set multiple extended properties on calendar item


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?


Solution

  • 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);