Search code examples
c#google-apigoogle-calendar-apigoogle-workspace

Google Calendar API v3 - Only send emails to newly added attendees


I am using Google Calendar API v3 for Google Workspace. I need to add attendees to existing events.

I use C# with official Google Calendar library. I have a service account with impersonation.

When I add an attendee, all attendees receive an notification according to SendUpdates request parameter. Apparently the API behaves correctly according to documentation.

But I need to send email only to a newly added attendee, to match UI experience.

var serviceCredential = GoogleCredential.FromJson(json)
    .CreateScoped("https://www.googleapis.com/auth/calendar.events")
    .CreateWithUser("[email protected]");

using var calendarService = new CalendarService(new BaseClientService.Initializer
{
    HttpClientInitializer = serviceCredential,
    ApplicationName = "AppName",
});

var evt = calendarService.Events.Get("<calendar_id>", "<event_id>").Execute();

evt.Attendees.Add(new EventAttendee()
{
    DisplayName = "Firstname Lastname",
    Email = "[email protected]"
});

// with calendarService.Events.Update, it also sends invitation to all attendees
var request = calendarService.Events.Patch(evt, CalendarId, evt.Id);
request.SendUpdates = EventsResource.PatchRequest.SendUpdatesEnum.ExternalOnly;
request.Execute();

Is there a way?


Solution

  • Sending Email only to the Attendees added on the Event

    With my research, it is an ongoing discussion on Google's Official Issue Tracker. Here is the link for it: Issue, I would suggest contributing with this thread to have a better chance of getting an answer from it sooner.

    Workaround:

    1st Suggestion: Instead of sending notifications through Google Calendar API utilize GMAIL API to send email to the added attendees. Note: This part won't perfectly provide replication from UI results however it will be closed enough depending on how you are going to create the email you will send.

    2nd Suggestion: Doing 3 API calls from Google Calendar API. 1st Call would be removing all attendees on the Event with no notification. Next is adding attendees with notification. Last is adding removed attendees without notification. This will be as close with the UI's behaviour however depending on the use case you might encounter some sort of runtime issues. Please do keep this in mind.

    Suggested Reference:

    GMAIL API. (messages.send)

    Calendar API (events.update)