Skip to main content Skip to footer

Saving Occurrences of Recurring Appointments in Scheduler for LightSwitch

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 :

  1. Create a distinct record in a separate table for each occurrence of a recurring appointment.
  2. Not create a separate record for non-recurring appointments.

Binding C1Scheduler to a Data Source

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.

Creating a Recurrence Table

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.

Saving Occurrences of Recurring Appointment

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.

MESCIUS inc.

comments powered by Disqus