How to add RecurrenceDay to the mask in a loop

5 posts, 0 answers
  1. Greg
    Greg avatar
    7 posts
    Member since:
    Nov 2011

    Posted 14 May 2013 Link to this post

    I'm looking for a way to add to the RecurrenceDay mask dynamically. I see that the Silverlight and WPF have AddDay functionality but I'm not having any luck finding it for the ASP.NET AJAX implementation.

    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!
  2. Boyan Dimitrov
    Admin
    Boyan Dimitrov avatar
    1746 posts

    Posted 17 May 2013 Link to this post

    Hello,

    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. 

    Kind regards,
    Boyan Dimitrov
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Greg
    Greg avatar
    7 posts
    Member since:
    Nov 2011

    Posted 20 May 2013 Link to this post

    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!!
        }
    }
  5. Greg
    Greg avatar
    7 posts
    Member since:
    Nov 2011

    Posted 20 May 2013 Link to this post

    I have found an oddity in the appointment.Description = appointmentDescription; line... The database field gets populated with NULL.

    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
  6. Boyan Dimitrov
    Admin
    Boyan Dimitrov avatar
    1746 posts

    Posted 23 May 2013 Link to this post

    Hello,

    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
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017