This is a migrated thread and some comments may be shown as answers.

Creating a Recurrencerule Code Behind C#

12 Answers 748 Views
Scheduler
This is a migrated thread and some comments may be shown as answers.
Philip
Top achievements
Rank 1
Philip asked on 21 Mar 2016, 10:43 AM

I am wanting to try and create a reoccurance rule through just code I found the guide of http://docs.telerik.com/devtools/aspnet-ajax/controls/scheduler/recurrence-editor/server-side-programming/populating-the-recurrence-rule by the way their is a spelling error at the top of the document in the h2 created.

I understand that I need to use recurrenncerange but I am saving my appointment via entity framework so i have two properties which i gather I need to populate.

 

RecurrenceRule

RecurrenceParentID

Can you please explain using the below methods if I can save the required information into the nvarchar that will allow the re ourrance data to show I am wanting to use this to show the days off that a staff memeber has.

 

01./Creating a Weekly Recurrence rule
02.int interval = 2;
03.RecurrenceRange range = new RecurrenceRange();
04.range.Start = DateTime.Now;
05.range.EventDuration = TimeSpan.FromMinutes(30);
06.range.MaxOccurrences = 3;
07.RecurrenceDay recurrenceDay = RecurrenceDay.Wednesday;
08.recurrenceEditor.RecurrenceRule = new WeeklyRecurrenceRule(interval, recurrenceDay, range);
09.//DayOfWeek startDayOfWeek = DayOfWeek.Tuesday;
10.//recurrenceEditor.RecurrenceRule = new WeeklyRecurrenceRule(interval, recurrenceDay, range, startDayOfWeek);

12 Answers, 1 is accepted

Sort by
0
Plamen
Telerik team
answered on 23 Mar 2016, 10:33 AM
Hi,

You can create the recurrence rule as it is shown in the sample page and then if you want to save it in the RecurrenceRule nvarchar field you can just use the toString method that will create the string of the rule.

As for the RecurrenceParentID it is not generated. You can assign it to null if the appointment has RecurrenceState master or not recurring and the id of the master if the appointment is with RecurrenceState exception. 

Hope this information will be helpful.

Regards,
Plamen
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
0
Philip
Top achievements
Rank 1
answered on 23 Mar 2016, 12:10 PM

I dont see their in that code or this url http://docs.telerik.com/devtools/aspnet-ajax/controls/scheduler/server-side-programming/working-with-recurring-appointments where the end date is set and also do you have examples of how one would set a daily, monthly rule and a weekly rule that only deals with an hourly rule ?.

 

0
Philip
Top achievements
Rank 1
answered on 23 Mar 2016, 12:49 PM

Thank you I managed to get the jist of the code however I have a couple of questions.

 

  1. 01.public string writeReocurranceRules(Constants.RecurrenceTypes recurrenceTypes, DateTime startDate, DateTime endDate, string subject)
    02.{
    03.            string rruleAsString = "";
    04.            // Create a sample appointment that starts at 6 / 1 / 2007 3:30 PM and lasts half an hour.
    05.            Telerik.Web.UI.Appointment recurringAppointment =
    06.            new Telerik.Web.UI.Appointment("1",
    07.              startDate.ToUniversalTime(),
    08.              endDate.ToUniversalTime(),
    09.              "Sample appointment");
    10. 
    11.            // Create a recurrence range, that specifies a limit of 10 occurrences for the appointment.
    12.            RecurrenceRange range = new RecurrenceRange();
    13.            range.Start = recurringAppointment.Start;
    14.            range.EventDuration = recurringAppointment.End - recurringAppointment.Start;
    15.            range.MaxOccurrences = 10;
    16. 
    17.            //create a recurrence that last for weekly it should last until the end date
    18.            if (recurrenceTypes == Constants.RecurrenceTypes.Weekly)
    19.            {
    20.                WeeklyRecurrenceRule rrule = new WeeklyRecurrenceRule(1, RecurrenceDay.Friday, range);
    21. 
    22.                Console.WriteLine("The appointment recurs at the following Dates");
    23.                foreach (DateTime occurrence in rrule.Occurrences)
    24.                {
    25.                    Console.WriteLine("\t{0}", occurrence);
    26.                }
    27. 
    28.                rruleAsString = rrule.ToString();
    29.            }
    30. 
    31.            //create a reccurance rule that is based of the start day it should last until the end date
    32.            if (recurrenceTypes == Constants.RecurrenceTypes.Monthly)
    33.            {                
    34.                MonthlyRecurrenceRule rrule = new MonthlyRecurrenceRule(startDate.Day, 2, range);
    35. 
    36.                Console.WriteLine();
    37. 
    38.                // Prints the string representation of the recurrence rule:
    39.                rruleAsString = rrule.ToString();
    40.                Console.WriteLine("Recurrence rule:\n\n{0}\n", rruleAsString);
    41.            }
    42.            //creeate a yearly recurrance it should last until the end date year.
    43.            if (recurrenceTypes == Constants.RecurrenceTypes.Yearly)
    44.            {
    45.                YearlyRecurrenceRule rrule = new YearlyRecurrenceRule(startDate.Month.Day, range);
    46. 
    47.            }
    48. 
    49.            // The string representation can be stored in a database, etc.
    50.            // ...
    51.            // Then it can be reconstructed using TryParse method:
    52.            RecurrenceRule parsedRule;
    53. 
    54.            RecurrenceRule.TryParse(rruleAsString, out parsedRule);
    55.            Console.WriteLine("After parsing (should be the same):\n\n{0}", parsedRule);
    56. 
    57.            return rruleAsString;
    58.}

 

  1. Can you explain what is the signification of the MaxOccurrences in range for example if you wanting to do a yearly or monthly range is that used?.
  2. Whats does the interval intail in each of the ranges for
       WeeklyRecurrenceRule rrule = new WeeklyRecurrenceRule(1, RecurrenceDay.Friday, range);
       MonthlyRecurrenceRule rrule = new MonthlyRecurrenceRule(startDate.Day, 2, range);
       YearlyRecurrenceRule rrule = new YearlyRecurrenceRule(startDate.Month.Day, range);
  3. When I try to send the month of a date time here in YearlyRecurrenceRule rrule = new YearlyRecurrenceRule(startDate.Month,startDate.Day, range); it says it cannot cast it to recurrenceMonth?.





0
Plamen
Telerik team
answered on 28 Mar 2016, 09:59 AM
Hello,

Straight to the points:
1) MaxOccurrences  is responsible for the amount of occurrences that will be generated by the recurrence rule.
2) The questions is not quite clear would you please elaborate it a little bit.

3)You need to pass the month as a Telerik.Web.UI.RecurrenceMonth type. It is an internal enumeration with the months of the year.

Regards,
Plamen
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
0
Philip
Top achievements
Rank 1
answered on 29 Mar 2016, 02:25 PM

1.How will i no maxOccurences until the algorithim produces the dates inbetween

 

2.How do i duplicate outlook style weekly monhtly and yearly options.

3 How can i pass the start date to get the Telerik.Web.UI.RecurrenceMonth value obv I want the start date to equal what month the appointment started on.

0
Plamen
Telerik team
answered on 01 Apr 2016, 08:19 AM
Hi,

Straight top the points.

1) it seems I could not explain properly - if you set the maxOccurences property it will generate this count of occurrences if you don't set it but rather use the other range properties you will not be able to get the occurrences until the rule is generated.

2)RadScheuler is using different recurrenceeditor layouts for Classic and Lite rendering so you can choose which ever you like better and use this RenderMode.

3)You can get the int of the month from the start date of the appointment and get the appropriate enum item as for example it is explained in this forum thread.

Regards,
Plamen
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
0
Philip
Top achievements
Rank 1
answered on 04 Apr 2016, 11:34 AM
Maybee I was not being clear. But surlery if ur setting a date range u dont need to set the maxocurrances?. Is their a more thoruough example of applying re ourrance in this mannner need it so the staff can manage shift patterns?.
0
Plamen
Telerik team
answered on 06 Apr 2016, 09:57 AM
Hello,

It is not quite clear what exactly are you trying to achieve. Would you please specify exactly what kind of recurrence are you trying to achieve and if it can be achieved by RadScheduler when its default recurrence editor is used?

Regards,
Plamen
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
0
Philip
Top achievements
Rank 1
answered on 06 Apr 2016, 10:05 AM
Its just the same options but im making a shift panel editor for the back end so they can set holidays and so forth for staff so im requring to insert the reccurance manually in the code behind. Say if a person was off every friday every week that be a weekly occurance or off monthly etc.
0
Plamen
Telerik team
answered on 11 Apr 2016, 07:13 AM
Hi,

In such scenario we recommend using the code provided in this help topic as a start to generate the desired recurrence rules. You may also add one test RadSchedulerRecurrenceEditor on the page and test if your code generates correct rule in it. Here is for example the code that I used to generate a recurrence rule that will repeat 3 times every Friday:
<telerik:RadSchedulerRecurrenceEditor id="RadSchedulerRecurrenceEditor1" runat="server"></telerik:RadSchedulerRecurrenceEditor>
int interval = 2;
     RecurrenceRange range = new RecurrenceRange();
     range.Start = DateTime.Now;
     range.EventDuration = TimeSpan.FromMinutes(30);
     range.MaxOccurrences = 3;
     RecurrenceDay recurrenceDay = RecurrenceDay.Friday;
     RadSchedulerRecurrenceEditor1.RecurrenceRule = new WeeklyRecurrenceRule(interval, recurrenceDay, range);

Hope this information will be helpful.

Regards,
Plamen
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
0
Philip
Top achievements
Rank 1
answered on 11 Apr 2016, 08:56 AM
That is great thank you that helped allot one question though I store days in a drop down of the full name how do i convert that to a RecurrenceDay comptable value ?
0
Plamen
Telerik team
answered on 12 Apr 2016, 10:44 AM
Hello,

You can refer to this forum thread where is shown once way to get the enum from and int value in C#.

Regards,
Plamen
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Tags
Scheduler
Asked by
Philip
Top achievements
Rank 1
Answers by
Plamen
Telerik team
Philip
Top achievements
Rank 1
Share this question
or