Skip to main content Skip to footer

Using Google Calendar with C1Scheduler for WPF

C1Scheduler is an out-of-the-box scheduling solution that comes with some very rich features such as recurring appointments, reminders, labels, data binding, grouping, and more. It allows users to create scheduling apps from scratch effortlessly.

In this article, we’ll look at how you can use the C1Scheduler for WPF and sync your appointments from one of the most popular web-based calendars (Google Calendar) to your desktop application.

icons google calendar

google calendar

We’ll begin by first adding the NuGet packages required for fetching the events from Google Calendar via Google Calendar APIs.

google calendar API

The Google Calendar library provides a CalendarService for interacting with events over a given time period. We’ll create an adapter that will wrap the CalendarService and provide Google Calendar events as C1Scheduler Appointments. The following shows the interface this adapter would be implementing:

public interface IAppointmentAdapter
{
    event EventHandler<SyncEventArgs> SyncStarting;
    event EventHandler SyncFinished;
    event EventHandler SyncFailed;
    AppointmentsResponse GetAppointments(RequestParameters requestParameters);
    void AddAppointment(Appointment appointment);
    void DeleteAppointment(Appointment appointment);
    void UpdateAppointment(Appointment appointment);
    Task Sync();
}

The diagram below shows the complete process the adapter handles.

adapter process

Next, let’s look at each of these steps in detail.

Get Events from Google Calendar

All communication with the Google Calendar API is done via an adapter that implements the IAppointmentAdapter:

public class GoogleEventAppointmentAdapter : IAppointmentAdapter
{

}

The request to fetch the events is constructed as follows:

public AppointmentsResponse GetAppointments(RequestParameters requestParameters)
{
  EventsResource.ListRequest request = _service.Events.List(_calendarId);
  request.TimeMin = requestParameters.MinDate;
  request.TimeMax = requestParameters.MaxDate;
  request.SingleEvents = requestParameters.SingleEvents;
  request.ShowDeleted = requestParameters.ShowDeleted;
  request.MaxResults = requestParameters.MaxResults;
  request.PageToken = requestParameters.PageToken;
  if (request.SingleEvents.Value)
  {
    request.OrderBy = requestParameters.OrderBy == OrderBy.StartTime ? EventsResource.ListRequest.OrderByEnum.StartTime : EventsResource.ListRequest.OrderByEnum.Updated;
  }
  var response = request.Execute();
  _fetchedEvents = response.Items;

 // Map fetched Events to Appointments
}

After the events have been fetched from the calendar, it needs to be mapped to the scheduler’s Appointment. The next section details this mapping.

Map Events to Appointments

The Google API provides an Event class that represents our appointments.

To import these events in the desktop application, the Event objects will be mapped to C1Scheduler’s Appointment objects as shown below:

google calendar evet

It will be implemented via the C# Expression as follows:

public static readonly Expression<Func<Event, Appointment>> AsAppointment = source => new Appointment()
{
  Subject = source.Summary,
  Body = source.Description,
  Location = source.Location,
  Label = new Label() { Color = GoogleCalendarColors.FirstOrDefault(x => x.Id == source.ColorId).Color },
  Start = source.Start.DateTime == null ? DateTime.Parse(source.Start.Date) : source.Start.DateTime.Value,
  End = source.End.DateTime == null ? DateTime.Parse(source.End.Date) : source.End.DateTime.Value
};
var appointments = _fetchedEvents.AsQueryable().Select(Mapper.AsAppointment).ToList();

Populate C1Scheduler’s AppointmentStorage

Appointments, which are equivalent to Google Calendar Events, are maintained in AppointmentStorage of C1Scheduler. After mapping the Events to Appointments, we can populate them in the storage as:

_appointmentsResponse = _syncManager.Adapter.GetAppointments(requestParameters);

_scheduler.BeginUpdate();
foreach (var appointment in _appointmentsResponse.Appointments)
{
  _scheduler.DataStorage.AppointmentStorage.Appointments.Add(appointment);
  appointment.Label = _scheduler.DataStorage.LabelStorage.Labels.FirstOrDefault(x => x.Color == appointment.Label.Color);
}
_scheduler.EndUpdate();

Adding, Updating, and Deleting Appointments

For user interactions with Appointments, C1Scheduler raises various events such as AppointmentAdded, BeforeAppointmentSave, and AppointmentDeleted. These events are the perfect place for the adapter to sync the changes back to Google Calendar.

private void SchedulerAppointmentAdded(object sender, AppointmentActionEventArgs e)
{
    _syncManager.Adapter.AddAppointment(e.Appointment);
}

private void SchedulerBeforeAppointmentSave(object sender, AppointmentActionEventArgs e)
{
    _syncManager.Adapter.UpdateAppointment(e.Appointment);
}
private void SchedulerAppointmentDeleted(object sender, AppointmentActionEventArgs e)
{
    _syncManager.Adapter.DeleteAppointment(e.Appointment);
}

The implementations to add, delete, and update via the CalendarService are as follows:

// Add a new appointment to google calendar.
_service.Events.Insert(Mapper.ConvertToEvent(appointment), _calendarId).Execute();

// Delete an existing appointment from google calendar.
_service.Events.Delete(_calendarId, GetMappedEventId(appointment)).Execute();

// Update an edited appointment to google calendar.
var editedEvent = Mapper.ConvertToEvent(appointment);
_service.Events.Update(editedEvent, _calendarId,  GetMappedEventId(appointment)).Execute();

That now completes the adapter. Use this adapter to add events from the Google calendar to any C1Scheduler instance. The following shows a sample application that makes use of this adapter.

adapter application

The complete sample can be found here.

Conclusion

C1Scheduler has built-in support for importing and exporting from popular file formats such as iCal.

Though there is no built-in support to connect with web-based calendars, its simple and extensible model is what popular web-based calendar APIs provide. As seen here, we can utilize this simple and extensible model to import and export events/appointments/meetings with such web-based calendars.

The adapters created in this blog have been added as a class library and can be used right away to connect your desktop schedulers with Google Calendar. You can also create your own adapters for other web-based calendars, such as Outlook, in a similar manner.

We would love to hear your thoughts–let us know if you found this useful.


Ankit Gupta

comments powered by Disqus