jamesiarmes / php-ews

PHP Exchange Web Services
http://jamesarmes.com/php-ews/
MIT License
567 stars 304 forks source link

php-ews get appointment by UID #508

Open dzyatko opened 6 years ago

dzyatko commented 6 years ago

Version (e.g. 1.0, dev-master): PHP version: Microsoft Exchange version: 2013SP1 Description of problem: Using php-ews: https://github.com/jamesiarmes/php-ews How can i get appointment by UID? It should by something like this?

Example request:

$uid = "040000008200E00074C5B7101A82E0080000000080EF61C1F5ECD3010000000000000000100000001C6157BE20CDCF428EE7871E5804FC35";
$item = new ItemIdType();
$item->UID = $uid;
$request->Items->CalendarItem->UID = $item;
phanos commented 5 years ago

I know this is a little late to the party but...

Here are some code snippets I've used in combination with impersonation to grab, update, and delete appointments out of user calendars in our organization. I use a utility function to build a vCal-Uid string based on UID, and then search using the MAPI property.

  /**
   * Wraps a string as vCal-Uid
   *
   * see http://msdn.microsoft.com/en-us/library/hh338153(v=exchg.80).aspx
   * for documentation on Exchange objects uid format for vCal-Uids
   *
   * @param string $s String to wrap
   *
   * @return string hex encoded vCal-Uid
   */
  public static function makeCalUID($s) {
    $GlobalIdData = '';
    $GlobalIdData .= self::asciiToHex('vCal-Uid');
    $GlobalIdData .= '01000000';
    $GlobalIdData .= self::asciiToHex($s) . '00';

    $DataSize      = dechex(strlen($GlobalIdData) / 2) . '000000';
    $ByteArrayId   = '040000008200E00074C5B7101A82E008';
    $Padding       = '0000000000000000';
    $InstanceYear  = '0000';
    $InstanceMonth = '00';
    $InstanceDay   = '00';

    $InstanceDate  = $InstanceYear . $InstanceMonth . $InstanceDay;

    $FileTime      = '0000000000000000';
    $CreationDateTime = $FileTime;
    $Header = $ByteArrayId . $InstanceDate . $CreationDateTime . $Padding . $DataSize;
    $EncodedGlobalId = $Header . $GlobalIdData;
    //$EncodedGlobalId = '' .
    //  . $ByteArrayId   //040000008200E00074C5B7101A82E008
    //  . $InstanceYear  //0000
    //  . $InstanceMonth //00
    //  . $InstanceDay   //00
    //  . $FileTime      //0000000000000000
    //  . $Padding       //0000000000000000
    //  . $DataSize      //dechex(strlen($GlobalIdData) / 2) . 000000
    //  . $GlobalIdData  //asciiToHex('vCal-Uid')
    //                   //01000000
    //                   //asciiToHex($s) . '00'
    //  . '';

    $returnValue = $EncodedGlobalId;
    return $returnValue;
  }

  /**
   * Converts ascii string to Hex
   *
   * @param string $s String to encode in hex
   *
   * @return string
   */
  public static function asciiToHex($s) {
    $returnValue = '';
    for ($i = 0; $i < strlen($s); $i++) {
      $returnValue .= dechex(ord($s[$i]));
    } unset($i);
    return $returnValue;
  }
/**
 * Searches calendar folder by UID
 *
 * @return EWS\Request\FindItemType finditem request
 *
 */
function createGetAppointmentByUIDRequest($uid) {
  $ms_0xCICAL = EWSUtils::makeCalUID($uid);

  $formattedUID = base64_encode(pack('H*', $ms_0xCICAL));

  $request = new EWS\Request\FindItemType();

  $request->ItemShape = new EWS\Type\ItemResponseShapeType();

  $request->ItemShape->BaseShape = EWS\Enumeration\DefaultShapeNamesType::ID_ONLY;

  // Build FindItem restriction.
  $extendedFieldURI = new EWS\Type\PathToExtendedFieldType();
  $extendedFieldURI->DistinguishedPropertySetId = EWS\Enumeration\DistinguishedPropertySetType::MEETING;
  $extendedFieldURI->PropertyId = 35;
  $extendedFieldURI->PropertyType = EWS\Enumeration\MapiPropertyTypeType::BINARY;

  $fieldURIOrConstant = new EWS\Type\FieldURIOrConstantType();
  $fieldURIOrConstant->Constant = new EWS\Type\ConstantValueType();
  $fieldURIOrConstant->Constant->Value = $formattedUID;

  $restriction = new EWS\Type\RestrictionType();
  $restriction->IsEqualTo = new EWS\Type\IsEqualToType();
  $restriction->IsEqualTo->ExtendedFieldURI = $extendedFieldURI;
  $restriction->IsEqualTo->FieldURIOrConstant = $fieldURIOrConstant;

  $request->Restriction = $restriction;

  // calendar
  $search_folder = new EWS\Type\DistinguishedFolderIdType();
  $search_folder->Id = EWS\Enumeration\DistinguishedFolderIdNameType::CALENDAR;

  $request->ParentFolderIds = new EWS\ArrayType\NonEmptyArrayOfBaseFolderIdsType();
  $request->ParentFolderIds->DistinguishedFolderId[] = $search_folder;

  $request->Traversal = EWS\Enumeration\ItemQueryTraversalType::SHALLOW;

  return $request;
}