Calendar Management in Cloud Calendars
Requirements: Cloud Calendars
Introduction
Cloud Calendars provides a suite of easy-to-use calendar and event management components. This article covers calendar management in popular cloud services, such as Office365 (Graph API) and Google.
Contents
GoogleCalendars
The GoogleCalendars component provides an easy-to-use interface to Google Calendars using Google's REST API. The GoogleCalendars component supports listing calendars and events as well as creating, updating, and deleting both calendars and events.
OAuth Authentication
Before using the component, an OAuth token must be provided through the Authorization property. The OAuth component included in the toolkit can be used to assist in this process.
By design, user-interaction is required to fetch an OAuth authorization string the first time. A user must be directed to a URL where they will authenticate and grant access to the connecting application. The user is then redirected back to the application along with a code that will be exchanged for an authorization string by the OAuth component. The authorization string is then used in requests to the cloud service.
The authorization string is valid only for a limited period of time; however, a parameter can be included in the initial authorization to request a refresh token that can be used without user interaction to update the authorization token. To add a parameter, the AddParam method can be used. In the following example, the "access_type" parameter is set to "offline".
Fetching an OAuth Authorization String Using the OAuth Component:
Oauth oauth = new Oauth();
oauth.ClientId = "CLIENT_ID"; // This is for testing purposes only
oauth.ClientSecret = "CLIENT_SECRET"; // This is for testing purposes only
oauth.ServerAuthURL = "https://accounts.google.com/o/oauth2/auth";
oauth.ServerTokenURL = "https://accounts.google.com/o/oauth2/token";
oauth.AuthorizationScope = "https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/calendar.events https://www.googleapis.com/auth/calendar.settings.readonly";
oauth.GrantType = OauthGrantTypes.ogtAuthorizationCode;
oauth.AddParam("access_type", "offline");
googlecalendar.Authorization = oauth.GetAuthorization();
Creating Calendars
Use the CreateCalendar method to create a new calendar. After creating a calendar with basic information, you can update it and set additional fields by editing the calendar in the Calendars collection and calling the UpdateCalendar method.
Creating a Calendar:
// Create a Calendar
googlecalendar.CreateCalendar("My personal Calendar");
// Set properties to update
googlecalendar.Calendars[0].Summary = "My personal Calendar 2";
googlecalendar.Calendars[0].Description = "Description";
// Update the Calendar
googlecalendar.UpdateCalendar(0);
Listing Calendars
Calendars for the user can be retrieved using the ListCalendars method. The CalendarList event will fire for each calendar, and the Calendars collection will be populated when the method returns. If the number of calendars exceeds the page size, the ListCalendarsMarker will be populated. You can call ListCalendars again to retrieve the next page of calendars. These calendars will be added to the end of the Calendars collection.
Listing a Calendar:
do {
googlecalendar.ListCalendars();
} while (googlecalendar.Config("ListCalendarsMarker").Length > 0);
Deleting Calendars
Calendars for the user can be deleted using the DeleteCalendar method.
Deleting a Calendar:
// List calendars and search for a calendar with the title "John's calendar"
googlecalendar.ListCalendars();
for (int i = 0; i < googlecalendar.Calendars.Count; i++) {
if (googlecalendar.Calendars[i].Summary == "John's calendar") {
//Delete the calendar
googlecalendar.DeleteCalendar(googlecalendar.Calendars[i].Id);
break;
}
}
Creating Events
Use the CreateEvent method to create a new event. The newly created event is added at the beginning of the Events collection. After creating the event with basic information, you can update it and set additional fields by editing the event in the Events collection and calling the UpdateEvent method.
Creating an Event:
googlecalendar.CreateEvent("John's Birthday", "2024-12-12T16:00:00", "2024-12-12T20:00:00", calID);
// Set properties to update
googlecalendar.Events[0].Summary = "John Doe's Birthday";
googlecalendar.Events[0].AttendeesCount = 2; // Number of attendees for the event
googlecalendar.Events[0].AttendeeIndex = 0; // Index of current attendee
googlecalendar.Events[0].AttendeeEmail = "person1@example.com";
googlecalendar.Events[0].AttendeeComment = "I'll be there";
googlecalendar.Events[0].AttendeeResponseStatus = TGCAttendeeResponseStatus.gcrsAccepted;
googlecalendar.Events[0].AttendeeDisplayName = "Attendee1";
googlecalendar.Events[0].AttendeeAdditionalGuests = 1;
googlecalendar.Events[0].AttendeeIndex = 1;
googlecalendar.Events[0].AttendeeEmail = "person2@example.com";
googlecalendar.Events[0].AttendeeDisplayName = "Attendee2";
googlecalendar.Events[0].AttendeeOptional = true;
// Set a reminder 15 minutes before
googlecalendar.Events[0].ReminderUseDefault = false;
googlecalendar.Events[0].RemindersOverridesCount = 1;
googlecalendar.Events[0].RemindersOverrideIndex = 0;
googlecalendar.Events[0].RemindersOverrideMethod = TGCRemindersOverrideMethods.gcrmEmail;
googlecalendar.Events[0].RemindersOverrideMinutes = 15;
// Update the event
googlecalendar.UpdateEvent(0);
Listing Events
Events for a specific calendar can be retrieved using the ListEvents method. The EventList event will fire for each event, and the Events collection will be populated once the method returns. If the number of events exceeds the page size, the ListEventsMarker will be populated. You can call ListEvent again to retrieve the next page of events. These events will be added at the end of the Events collection.
Listing an Event:
do {
googlecalendar.ListEvents(calendarId);
} while (googlecalendar.ListEventsMarker.Length > 0);
A specific event can also be retrieved using the GetEvent method.
Retrieving an Event by ID:
// Retrieve an event by its ID
string calendarId = "12345"; // Replace with the actual calendar ID
string eventId = "54321"; // Replace with the actual event ID
googlecalendar.GetEvent(calendarId, eventId);
// Access the retrieved event
GCalEvent retrievedEvent = googlecalendar.Events[googlecalendar.Events.Count - 1];
retrievedEvent.Summary; // event name
retrievedEvent.StartDateTime; // start dateTime of event
retrievedEvent.Description; // description of event
retrievedEvent.Created; // event creation time
googlecalendar.GetEventField(googlecalendar.Events.Count - 1, "/json/summary"); // event name
googlecalendar.GetEventField(googlecalendar.Events.Count - 1, "/json/end"); // end dateTime of event
Deleting Events
Events for the user can be deleted using the DeleteEvent method.
Deleting an Event:
// List events and delete all events containing "Meeting" in their name
googlecalendar.ListEvents(calendarId);
for (int i = 0; i < googlecalendar.Events.Count; i++) {
if (googlecalendar.Events[i].Summary.Contains("Meeting")) {
googlecalendar.DeleteEvent(calID, googlecalendar.Events[i].Id);
}
}
Office365Calendars
The Office365 component provides an easy-to-use interface to calendar management using Microsoft's Graph API. The Office365 component supports listing calendars and events as well as creating, updating, and deleting both calendars and events.
OAuth Authentication
Before using the component, an OAuth token must be provided through the Authorization property. The OAuth component included in the toolkit can be used to assist in this process.
By design, user interaction is required to fetch an OAuth authorization string the first time. A user must be directed to a URL where they will authenticate and grant access to the connecting application. The user is then redirected back to the application along with a code that will be exchanged for an authorization string by the OAuth component. The authorization string is then used in requests to the cloud service.
The authorization string is valid only for a limited period of time; however, a parameter can be included in the initial authorization to request a refresh token that can be used without user interaction to update the authorization token.
Fetching an OAuth Authorization String Using the OAuth Component:
Oauth oauth = new Oauth();
oauth.ClientId = "CLIENT_ID"; // This is for testing purposes only
oauth.ClientSecret = "CLIENT_SECRET"; // This is for testing purposes only
oauth.ServerAuthURL = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize";
oauth.ServerTokenURL = "https://login.microsoftonline.com/common/oauth2/v2.0/token";
oauth.AuthorizationScope = "offline_access calendars.readwrite user.read";
oauth.GrantType = OauthGrantTypes.ogtAuthorizationCode;
office365.Authorization = oauth.GetAuthorization();
Creating Calendars
Use the CreateCalendar method to create a new calendar. After creating a calendar with basic information, you can update it and set additional fields by editing the calendar in the Calendars collection and calling the UpdateCalendar method.
Creating and Updating a Calendar:
office365.CreateCalendar("My Calendar");
// Set properties to update
office365.Calendars[0].Color = TOfficeColors.occLightGreen;
office365.Calendars[0].Name = "New name";
office365.UpdateCalendar(0);
Listing Calendars
Calendars for the user can be retrieved using the ListCalendars method. The CalendarList event will fire for each calendar, and the Calendars collection will be populated when the method returns. If the number of calendars exceeds the page size, the ListCalendarsMarker will be populated. You can call ListCalendars again to retrieve the next page of calendars. These calendars will be added to the end of the Calendars collection.
Listing a Calendar:
do {
office365.ListCalendars();
} while (office365.Config("ListCalendarsMarker").Length > 0);
Deleting Calendars
Calendars for the user can be deleted using the DeleteCalendar method.
Deleting a Calendar:
// List calendars and delete the calendar named "MyCalendar"
office365.ListCalendars();
for (int i = 0; i < office365.Calendars.Count; i++) {
if (office365.Calendars[i].Name == "MyCalendar") {
office365.DeleteCalendar(office365.Calendars[i].Id);
break;
}
}
Creating Events
Use the CreateEvent method to create a new event. The newly created event is added at the beginning of the Events collection. After creating the event with basic information, you can update it and set additional fields by editing the event in the Events collection and calling the UpdateEvent method.
Creating an Event:
office365.CreateEvent("My event", "2024-11-08T16:00:00", "2024-11-08T17:00:00", office365.Calendars[0].Id);
// Set properties to update
office365.Events[0].Subject = "new event name";
office365.Events[0].AttendeesCount = 2;
office365.Events[0].AttendeeIndex = 0;
office365.Events[0].AttendeeEmail = "person1@example.com";
office365.Events[0].AttendeeStatusResponse = TOCAttendeeStatusResponses.oasrAccepted;
office365.Events[0].AttendeeType = TOCAttendeeTypes.ocatOptional;
office365.Events[0].AttendeeIndex = 1;
office365.Events[0].AttendeeEmail = "person2@example.com";
office365.Events[0].AttendeeStatusResponse = TOCAttendeeStatusResponses.oasrAccepted;
office365.Events[0].AttendeeType = TOCAttendeeTypes.ocatRequired;
// Set a reminder 15 minutes before
office365.Events[0].IsReminderOn = true;
office365.Events[0].ReminderMinutesBeforeStart = 15;
office365.UpdateEvent(0);
Listing Events
Events for a specific calendar can be retrieved using the ListEvents method. The EventList event will fire for each event, and the Events collection will be populated once the method returns. If the number of events exceeds the page size, the ListEventsMarker will be populated. You can call ListEvent again to retrieve the next page of events. These events will be added at the end of the Events collection.
Listing an Event:
do {
office365.ListEvents(office365.Calendars[0].Id);
} while (office365.ListEventsMarker.Length > 0);
A specific event can also be retrieved using the GetEvent method.
Retrieving an Event by ID:
// Retrieve an event by its ID
string eventId = "12345"; // Replace with the actual event ID
office365.GetEvent(eventId);
// Access the retrieved event
OCalEvent retrievedEvent = office365.Events[0];
retrievedEvent.Subject; //event subject
retrievedEvent.StartDateTime; //start dateTime of event
retrievedEvent.AttendeeStatusResponse; //attendee status response
office365.GetEventField(office365.Events.Count - 1, "/json/subject"); //event name
office365.GetEventField(office365.Events.Count - 1, "/json/isCancelled"); //if the event is cancelled
Deleting Events
Events for the user can be deleted using the DeleteEvent method.
Deleting an Event:
// List events and delete the event named "new event name"
office365.ListEvents(office365.Calendars[0].Id);
for (int i = 0; i < office365.Events.Count; i++) {
if (office365.Events[i].Subject == "new event name") {
office365.DeleteEvent(office365.Events[i].Id);
break;
}
// Note: the component would let you name more than one event with the same name so the example would delete the first encounter with the event that has the specified name
}
We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.