Billy Faith Susanto
Billy Faith Susanto

Reputation: 45

How to update attendees list in Google Calendar Event?

This code below is to update title, detail, and location:

$event->setSummary($_POST['title']);
$event->setDescription($_POST['detail']);
$event->setLocation($_POST['location']);

This code below is to update date (start and end):

$start = new Google_Service_Calendar_EventDateTime();
$start->setTimeZone($timezone);
$start->setDateTime($startDateTime);
$event->setStart($start);

$end = new Google_Service_Calendar_EventDateTime();
$end->setTimeZone($timezone);
$end->setDateTime($endDateTime);
$event->setEnd($end);

But I'm struggling to update the attendees list. For the insert one is below:

$people = $_POST['people'];  // POST from other webpage
$finalpeople = [];
foreach ($people as $person) {
    $finalpeople[] = ['email' => $person];
}
$data['result'] = $finalpeople;

// just look at the attendees one
$event = new Google_Service_Calendar_Event(array(
    'id'=>  $idFinal,
    'summary' => $_POST['title'],
    'location' => $_POST['location'],
    'description' => $_POST['detail'],
    'start' => array(
      'dateTime' => $startDateTime,
      'timeZone' => $_POST['timezone'],
    ),
    'end' => array(
      'dateTime' => $endDateTime,
      'timeZone' =>  $_POST['timezone'],
    ),
    'attendees' => $data['result'],
    'reminders' => array(
      'useDefault' => FALSE,
      'overrides' => array(
        array('method' => 'email', 'minutes' => 24 * 60),
        array('method' => 'popup', 'minutes' => $_POST['reminder']),
      ),
    ),
  ));

Anyone have any idea to update the attendees?

Upvotes: 1

Views: 998

Answers (1)

Tanaike
Tanaike

Reputation: 201358

I believe your goal as follows.

  • You want to update attendees in an event using googleapis with PHP.
  • You have already been able to get and put values for Google Calendar with Calendar API.

For this, how about this answer? In this case, I would like to propose to use the method of Events: patch in Calendar API.

Sample script:

$client = getClient();
$calendar = new Google_Service_Calendar($client);

$calendarId = "###";  // Please set the calendar ID.
$eventId = "###";  // Please set the event ID.

// Here, please set the new attendees.
$newAttendees = [
    array(
        'email' => '###',
        'comment' => 'sample 1'
    ),
    array(
        'email' => '###',
        'comment' => 'sample 2'
    )
];

$event = new Google_Service_Calendar_Event;
$event->attendees = $newAttendees;
$result = $calendar->events->patch($calendarId, $eventId, $event);

Note:

  • When you use $newAttendees, the attendees of the existing event are overwritten. So please be careful this. So I recommend to use a sample event for testing above script.

Reference:

Upvotes: 1

Related Questions