Managing calendars and events is crucial for many applications, especially when integrating with popular services like Google Calendar and Microsoft 365. Starting with 4D 20 R9, you can easily retrieve and access calendars and events from these platforms with the new 4D NetKit commands. Whether you’re synchronizing schedules, building calendar-based features, or simply fetching event details, these commands enable seamless integration with Google and Microsoft 365 services.
In the examples below, we will retrieve the list of all the calendars in a Google Calendar or Microsoft 365 account and all the events associated with the first calendar in the list.
Account connection
As usual, before using Google or Microsoft API, we will create an OAuth 2.0 connection object:
For Google API:
var $oAuth2 : cs.NetKit.OAuth2Provider
var $google : cs.NetKit.Google
$oAuth2:=cs.NetKit.OAuth2Provider.new($googleCredentials)
$google:=cs.NetKit.Google.new($oAuth2)
For Microsoft API:
var $oAuth2: cs.NetKit.OAuth2Provider
var $office365 : cs.NetKit.Office365
$oAuth2:=cs.NetKit.OAuth2Provider.new($microsoftCredentials)
$office365:=cs.NetKit.Office365.new($oAuth2)
Get Calendars list
Before retrieving events of a specific calendar, you need to get the calendar Id. To get it, you can use the .calendar.getCalendars() function to list all the calendars linked to your account. Both APIs provide a method to list calendars, but the parameters and the object returned may differ between Google and Microsoft.
For Google API
// Gets all the calendars
var $calendars:=$google.calendar.getCalendars()
// For the rest of the example, we'll use the first calendar in the list
var $myCaldendar:=$calendars.calendars[0]
for Microsoft API
// Gets all the calendars
var $calendars:=$office365.calendar.getCalendars()
// For the rest of the example, we'll use the first calendar in the list
var $myCaldendar:=$calendars.calendars[0]
get eventS list
We can now retrieve the events linked to a specific calendar with the calendar.getEvents() function. Both APIs provide a method to list events, but the parameters and the object returned may differ between Google and Microsoft.
for google API
// Calculates the date range to be used
var $startDate:={date:Current date(); time:?00:00:00?}
var $endDate:={date:Current date()+7); time:?23:59:59?}
// Gets all the event of the selected calendars
var $events:=$google.calendar.getEvents({calendarId: $myCalendar.id; top: 100; singleEvents: True; startDateTime: $startDate; endDateTime: $endDate})
for Microsoft API
// Calculates the date range to be used
var $startDate:={date:Current date(); time:?00:00:00?}
var $endDate:={date:Current date()+7); time:?23:59:59?}
// Gets all the event of the selected calendars
var $events:=$office365.calendar.getEvents({calendarId: $myCalendar.id; top: 100; startDateTime: $startDate; endDateTime: $endDate})
Conclusion
Managing calendars and events with the new 4D NetKit commands in 4D 20 R9 is easy. Whether you’re working with Google Calendar or Microsoft 365, you can quickly retrieve calendars and events while accounting for the differences in how the two platforms work. For more details, don’t forget to check out the 4D NetKit documentation!
And this is just the beginning, more commands and features are coming soon to make your calendar integrations even more powerful!