My goal is to loop through multiple weekdays defined in a database and add them to the WeeklyRecurrenceRule DayOfWeekMask when creating a new appointment in the RadScheduler.
Thanks!
5 Answers, 1 is accepted
I would suggest reviewing the following help article that shows how you can work with the recurrence rule of specific appointment. For example you may implement your custom logic in the RadScheduler FormCreated server-side event handler.
Boyan Dimitrov
the Telerik team
Thank you for the response, I have reviewed all of the documentation I could find including the link you posted. I'm sure my solution is unique so I'll post the working code below. The scenario I was up against was a start date and time, end date and time and the need to loop through the days of the week to populate the mask used in building the recurrance rule. This solution could be tooled to use a delimited string of days during the week also. (e.g. 1,3,5,7 for Mon, Wed, Fri, Sun). I hope my solution or parts of it can help anybody looking for this flexibility.
-Greg
foreach
(DataRow dr
in
ds.Tables[0].Rows)
// LOOP THROUGH THE SCHEDULE SEGMENTS RETURNED BY THE DATABASE FUNCTION
{
bool
errors =
false
;
// BUILD THE APPOINTMENT FOR THE SCHEDULE SEGMENT
if
(!
string
.IsNullOrEmpty(dr[
"staffTeamName"
].ToString()))
// Check the row for data before proceeding
{
// The RadScheduler needs the Start and End Date to be the same (but not the time) for the recurring appointment,
// UNLESS THE START AND END SPANS MIDNIGHT - which is handled below
// Appointment Start Date and Time
string
appointmentStart = String.Format(
"{0:MM/dd/yyyy}"
, Convert.ToDateTime(dr[
"scheduleSegmentStartDate"
]));
appointmentStart +=
" "
+ String.Format(
"{0:HH:mm:ss}"
, Convert.ToDateTime(dr[
"watchStartTime"
]));
// Appointment Start Date and Time
string
appointmentEnd = String.Format(
"{0:MM/dd/yyyy}"
, Convert.ToDateTime(dr[
"scheduleSegmentStartDate"
]));
appointmentEnd +=
" "
+ String.Format(
"{0:HH:mm:ss}"
, Convert.ToDateTime(dr[
"watchEndTime"
]));
// Start the appointment build - This holds the basic information for any appointment, recurring or not
Appointment appointment =
new
Appointment();
appointment.Start = Convert.ToDateTime(appointmentStart);
//DateTime.Today.AddDays(1).AddHours(9.5),
appointment.End = Convert.ToDateTime(appointmentEnd);
//DateTime.Today.AddDays(1).AddHours(12),
// Appointment Subject
string
appointmentSubject = dr[
"staffTeamName"
].ToString();
appointment.Subject = appointmentSubject;
// Appointment Description
string
appointmentDescription = dr[
"watchName"
].ToString().Trim();
appointmentDescription +=
" - "
+ dr[
"staffTeamName"
].ToString().Trim();
appointment.Description = appointmentDescription;
// For the additional detail fields that have been added to the basic RadSchedule table schema
appointment.Attributes[
"scheduleStaffID"
] = staffID.ToString();
appointment.Attributes[
"scheduleSegmentID"
] = dr[
"scheduleSegmentID"
].ToString();
appointment.Attributes[
"scheduleTypeID"
] =
"1"
;
// Type 1 is the Normal event type
appointment.Attributes[
"scheduleDateAdded"
] = DateTime.Now.ToString();
appointment.Attributes[
"scheduleDateModified"
] = DateTime.Now.ToString();
appointment.Attributes[
"scheduleModifiedBy"
] =
"1"
;
// This will be the admin user after authentication is enabled
// Build the Recurrence Rule
RecurrenceRange range =
new
RecurrenceRange();
range.Start = appointment.Start;
// Check to see if the End time is after midnight
if
((appointment.End - appointment.Start).TotalHours < 0)
{
appointment.End = appointment.End.AddDays(1);
// The end time is after midnight so add one day to the end date
}
// The number of hours the appointment lasts - has to be positive
range.EventDuration = appointment.End - appointment.Start;
// Initialize start and end day of week Integer (Monday=1 Sunday=7)
int
weekDayStartInt = Convert.ToInt32(dr[
"weekdayStartInt"
]);
int
weekDayEndInt = Convert.ToInt32(dr[
"weekdayEndInt"
]);
// Count of days between start weekday and end weekday taking into account that the end date might wrap around Sunday (e.g. Fri, Sat, Sun, Mon)
int
daysDiff = (((7 + (weekDayEndInt - weekDayStartInt)) % 7) + 1);
// Populate an array with the days of the week - used to construct the RecurrenceRule using bitwise operations
int
[] weekDay =
new
int
[daysDiff];
// Initialize the array that will hold the weekday day number - Zero based array, of course (Monday=1 Sunday=7)
for
(
int
i = 0; i < daysDiff; i++)
{
if
(weekDayStartInt + i > 7)
{
weekDay[i] = (weekDayStartInt + i) - 7;
// The weekday end is before the weekday start (e.g. Start: Sunday End: Tuesday)
}
else
{
weekDay[i] = (weekDayStartInt + i);
// the weekday end is after the weekday start (e.g. Start: Monday End: Wednesday)
}
}
// Instantiate the DayOfWeekMask that's used in the WeeklyRecurrenceRule
RecurrenceDay DayOfWeekMask =
new
RecurrenceDay();
// Construct the DaysOfWeekMask using weekDay array and the OR bitwise operator - this is used in the WeeklyRecurrenceRule below
for
(
int
i = 0; i < daysDiff; i++)
{
//countDays += CountDays(Convert.ToDateTime(dr["scheduleSegmentStartDate"]), Convert.ToDateTime(dr["scheduleSegmentEndDate"]), weekDay[i]); DEPRICATED! 05/16/2013
switch
(weekDay[i])
{
case
1: DayOfWeekMask = DayOfWeekMask | RecurrenceDay.Monday;
break
;
case
2: DayOfWeekMask = DayOfWeekMask | RecurrenceDay.Tuesday;
break
;
case
3: DayOfWeekMask = DayOfWeekMask | RecurrenceDay.Wednesday;
break
;
case
4: DayOfWeekMask = DayOfWeekMask | RecurrenceDay.Thursday;
break
;
case
5: DayOfWeekMask = DayOfWeekMask | RecurrenceDay.Friday;
break
;
case
6: DayOfWeekMask = DayOfWeekMask | RecurrenceDay.Saturday;
break
;
case
7: DayOfWeekMask = DayOfWeekMask | RecurrenceDay.Sunday;
break
;
}
}
// Set the recurring appointment's end date - The "until date" property is NOT inclusive so... Add a day!
range.RecursUntil = Convert.ToDateTime(dr[
"scheduleSegmentEndDate"
]).AddDays(1);
// Set the RecurrenceRule as Weekly and pass the Interval, Days of Week it will ocurr and the range property value set
WeeklyRecurrenceRule rule =
new
WeeklyRecurrenceRule(1, DayOfWeekMask, range);
// Convert the RecurrenceRule to a string
appointment.RecurrenceRule = rule.ToString();
// Insert the Appointment into the RadScheduler
this
.RadScheduler1.InsertAppointment(appointment);
}
else
{
errors =
true
;
}
if
(!errors)
{
// CONFIRM SUCCESS
}
else
{
// LOG THE ERROR AND NOTIFIY THE ADMIN!!
}
}
I have a workaround by using appointment.Attributes["scheduleDescription"] = appointmentDescription.
The question is why doesn't the appointment.Description property work?
I'm using Telerik.Web.UI, v.2013.1.417.40 (Dev)
Thanks,
Greg
Indeed the description field in the data base is not properly updated when RadScheduler InsertAppointment method is used for inserting an appointment. We would need a bit more time to investigate that issue and I will post any update here as soon as possible.
Regards,
Boyan Dimitrov
Telerik
here is what I came up with
Dim range As RecurrenceRange = New RecurrenceRange()
range.EventDuration = EndTime.Subtract(StartTime)
range.Start = StartTime
range.RecursUntil = CDate(dr(
"RecurEndDate"
)).Add(difference)
'range.MaxOccurrences = 1000
Dim recDay As Integer = 0
Select Case (dr(
"Period"
)) 'guessing the correct day that matches these masks
Case
"1000000"
: recDay = RecurrenceDay.Sunday
Case
"0100000"
: recDay = RecurrenceDay.Monday
Case
"0010000"
: recDay = RecurrenceDay.Tuesday
Case
"0001000"
: recDay = RecurrenceDay.Wednesday
Case
"0000100"
: recDay = RecurrenceDay.Thursday
Case
"0000010"
: recDay = RecurrenceDay.Friday
Case
"0000001"
: recDay = RecurrenceDay.Saturday
Case
"1111111"
: recDay = RecurrenceDay.EveryDay
Case
"1000001"
: recDay = RecurrenceDay.WeekendDays
Case
"0111110"
: recDay = RecurrenceDay.WeekDays
Case Else
Dim mask As String = dr(
"Period"
)
If mask.Substring(0, 1) =
"1"
Then recDay = recDay Or RecurrenceDay.Sunday
If mask.Substring(1, 1) =
"1"
Then recDay = recDay Or RecurrenceDay.Monday
If mask.Substring(2, 1) =
"1"
Then recDay = recDay Or RecurrenceDay.Tuesday
If mask.Substring(3, 1) =
"1"
Then recDay = recDay Or RecurrenceDay.Wednesday
If mask.Substring(4, 1) =
"1"
Then recDay = recDay Or RecurrenceDay.Thursday
If mask.Substring(5, 1) =
"1"
Then recDay = recDay Or RecurrenceDay.Friday
If mask.Substring(6, 1) =
"1"
Then recDay = recDay Or RecurrenceDay.Saturday
End Select
Dim rule As WeeklyRecurrenceRule = New WeeklyRecurrenceRule(dr(
"Every"
), recDay, range)
If arExceptions.Count > 0 Then
For Each item As String In arExceptions
rule.Exceptions.Add(CDate(item).Add(range.Start.TimeOfDay().Add(excDiffernce)))
Next
End If
apt.RecurrenceRule = rule.ToString()