In C1Scheduler, an appointment (simple or recurring) is saved as a single record when stored in a database. Many times users want to store all the instances of recurring appointment in a separate table instead of saving it as a single record. In this blog lets discuss simple approach to create a distinct record in a separate table for each occurrence of a recurring appointment. We'll handle following scenarios :
The first step is to bind the C1Scheduler to a data source. We will follow the steps as it is to bind the scheduler to a simple Appointment table using the steps mentioned in the documentation.
In the database designer, the recurrence table needs an extra field to reference the master appointment (Parent). If the master appointments table uses a GUID instead of an integer as the id column, then the recurrence table needs to follow suit.
In the screen designer of the CalendarScreen, click Add Data Item and add the entity for this table (Recurrences). Now create a handler for Saved event of the screen. The Saved handler for the screen looks like this:
partial void Calendar_Saved()
{
this.Recurrences.Refresh();
Dispatchers.Main.BeginInvoke(() =>
{
DataWorkspace workspace = this.Application.CreateDataWorkspace();
C1.C1Schedule.AppointmentCollection acoll = scheduler.DataStorage.AppointmentStorage.Appointments;
foreach (C1.C1Schedule.Appointment a in acoll)
{
if (a.IsRecurring)
{
int key = (int)a.Key[0];
if (!this.Recurrences.Any(r => r.Parent == key))
{
C1.C1Schedule.AppointmentList alist = acoll.GetOccurrences(a, a.Start, a.Start.AddYears(1));
foreach (C1.C1Schedule.Appointment aa in alist)
{
Recurrence rec = workspace.ApplicationData.Recurrences.AddNew();
rec.Subject = aa.Subject;
rec.StartTime = aa.Start;
rec.EndTime = aa.End;
rec.Location = aa.Location;
rec.Body = aa.Body;
rec.Properties = aa.GetAppointmentProperties();
rec.Parent = key;
}
}
}
}
workspace.ApplicationData.Details.Dispatcher.BeginInvoke(() =>
{
workspace.ApplicationData.SaveChanges();
});
});
}
Download the sample for detailed implementation.