# RecurrenceRule

**RadScheduleView** includes support for recurring events on daily, weekly, monthly and yearly basis. Exceptions to the recurrence rules are also permitted. To support this recurrence behavior, the **IAppointment** interface includes the **RecurrenceRule** property. When an appointment is promoted into a recurring event its **RecurrenceRule** is set with correct [RecurrencePattern](https://www.telerik.com/products/wpf/documentation/controls/radscheduleview/features/recurrence/recurrences-recurrencepattern).

The **RecurrenceRule** class is the engine for creating and evaluating recurrence rules.

The purpose of this tutorial is to show you:

* The main properties exposed by the **RecurrenceRule** class:

  * [RecurrencePattern](https://www.telerik.com/products/wpf/documentation/controls/radscheduleview/features/recurrence/recurrences-recurrencerule#recurrencepattern)

  * [Exceptions](https://www.telerik.com/products/wpf/documentation/controls/radscheduleview/features/recurrence/recurrences-recurrencerule#exceptions)

* [How to create a recurrence rule and associate it with an appointment](https://www.telerik.com/products/wpf/documentation/controls/radscheduleview/features/recurrence/recurrences-recurrencerule#how-to-create-a-recurrence-rule-and-associate-it-with-an-appointment)

* [How to add exception occurrences to the recurrence rule](https://www.telerik.com/products/wpf/documentation/controls/radscheduleview/features/recurrence/recurrences-recurrencerule#how-to-add-exception-occurrences-to-the-recurrence-rule)

## RecurrenceRule Class

While the **RecurrencePattern** class carries the main information about the occurrence, the **RecurrenceRule** class is the engine for creating and evaluating recurrence rules.

The **RecurrenceRule** class is located in the **Telerik.Windows.Controls.ScheduleView** namespace of the **Telerik.Windows.Controls.ScheduleView\.dll** assembly.

The next several sections describe which are the main properties and methods exposed by the **RecurrenceRule** class.

## RecurrencePattern

The **RecurrenceRule** class exposes a **RecurrencePattern** property, which allows you to get\set the recurrence pattern associated with the current rule. For more information about the **RecurrencePattern**, take a look at the [RecurrencePattern](https://www.telerik.com/products/wpf/documentation/controls/radscheduleview/features/recurrence/recurrences-recurrencepattern) topic, which is entirely dedicated to recurrence patterns.

## Exceptions

The **RecurrenceRule** class exposes an **Exceptions** property, which allows you to get or set all exception occurrences associated with the current rule. For more information read [How to Add Exception Occurrences to the Recurrence Rule](https://www.telerik.com/products/wpf/documentation/controls/radscheduleview/features/recurrence/recurrences-recurrencerule#how-to-add-exception-occurrences-to-the-recurrence-rule)

## How to Create a Recurrence Rule and Associate it With an Appointment

The **RecurrenceRule** class has only one public constructor, which accepts a **RecurrencePattern** as a parameter.

> tip The **RecurrencePattern** is a mandatory part of the **RecurrenceRule**. Which means that when creating a new **RecurrenceRule** you must associate the rule with a recurrence pattern.

```C#
var startDate = new DateTime(2011, 05, 11, 10, 0, 0);
var fitnessAppointment = new Appointment()
{
    Start = startDate,
    End = startDate.AddHours(2),
    Subject = "Fitness"
};
var recurrencePattern = new RecurrencePattern()
{
    Frequency = RecurrenceFrequency.Weekly,
    MaxOccurrences = 30,
    DaysOfWeekMask = RecurrenceDays.Monday | RecurrenceDays.Wednesday | RecurrenceDays.Friday
};
fitnessAppointment.RecurrenceRule = new RecurrenceRule(recurrencePattern);
```

```VB.NET
	Dim startDate = New DateTime(2011, 5, 11, 10, 0, 0)
	Dim fitnessAppointment = New Appointment() With {
	    .Start = startDate,
	    .[End] = startDate.AddHours(2),
	    .Subject = "Fitness"
	}
	Dim recurrencePattern = New RecurrencePattern() With {
	    .Frequency = RecurrenceFrequency.Weekly,
	    .MaxOccurrences = 30,
	    .DaysOfWeekMask = RecurrenceDays.Monday Or RecurrenceDays.Wednesday Or RecurrenceDays.Friday
	}
	fitnessAppointment.RecurrenceRule = New RecurrenceRule(recurrencePattern)
```

The above example shows you how to create a recurrence pattern, then how to associate it with a recurrence rule. Finally the recurrence rule is assigned to an appointment.

## How to Add Exception Occurrences to the Recurrence Rule

**RadScheduleView's** API permits you to add exception occurrences to the recurrence rule. In order to add exceptions, you should use the **RecurrenceRule**'s **AddException()** method. The **AddException()** method has two overloads.

The first one takes two parameters:

* The first parameter is a **DateTime** which indicates when the exception occurs.

* The second parameter is an **IAppointment** instance. This is the exception appointment.

When you want to add an exception to a recurrence you need to use this method.

The second overload takes only one argument - a **DateTime** parameter. The purpose of this overload is to add an exception by **removing** a given occurrence from a recurrence.

One possible scenario of adding an exception to a recurrence rule is shown in the next example:

* Create a new appointment.

```C#
var startDate = new DateTime(2011, 05, 11, 10, 0, 0);
var fitnessAppointment = new Appointment()
{
    Start = startDate,
    End = startDate.AddHours(2),
    Subject = "Fitness"
};
```

```VB.NET
	Dim startDate = New DateTime(2011, 5, 11, 10, 0, 0)
	Dim fitnessAppointment = New Appointment() With {
	    .Start = startDate,
	    .[End] = startDate.AddHours(2),
	    .Subject = "Fitness"
	}
```

* Create a [RecurrencePattern](https://www.telerik.com/products/wpf/documentation/controls/radscheduleview/features/recurrence/recurrences-recurrencepattern).

```C#
var recurrencePattern = new RecurrencePattern()
{
    Frequency = RecurrenceFrequency.Daily,
    DaysOfWeekMask = RecurrenceDays.Wednesday,
    MaxOccurrences = 10
};
```

```VB.NET
	Dim recurrencePattern = New RecurrencePattern() With {
	    .Frequency = RecurrenceFrequency.Daily,
	    .DaysOfWeekMask = RecurrenceDays.Wednesday,
	    .MaxOccurrences = 10
	}
```

* [Create](https://www.telerik.com/products/wpf/documentation/controls/radscheduleview/features/recurrence/recurrences-recurrencerule#how-to-create-a-recurrence-rule-and-associate-it-with-an-appointment) a **RecurrenceRule** and associate the create recurrence pattern with it.

```C#
var rule = new RecurrenceRule(recurrencePattern);
```

```VB.NET
	Dim rule = New RecurrenceRule(recurrencePattern)
```

* Add an exception occurrence to the recurrence rule.

```C#
var exceptionDate = fitnessAppointment.Copy();

exceptionDate.Start = fitnessAppointment.Start.AddDays(-1);
exceptionDate.End = fitnessAppointment.End.AddDays(-1);
rule.AddException(startDate, exceptionDate);
```

```VB.NET
	Dim exceptionDate = fitnessAppointment.Copy()
	exceptionDate.Start = fitnessAppointment.Start.AddDays(-1)
	exceptionDate.[End] = fitnessAppointment.[End].AddDays(-1)
	rule.AddException(startDate, exceptionDate)
```

* Associate the create recurrence rule with the appointment.

```C#
fitnessAppointment.RecurrenceRule = rule;
```

```VB.NET
	fitnessAppointment.RecurrenceRule = rule
```

When adding the created appointment to the **RadScheduleView**'s **AppointmentsSource** collection the result should be similar to the snapshot below.

![WPF RadScheduleView Recurrence Rule](https://www.telerik.com/products/wpf/documentation/assets/55b58ac61448f61260df52d397687fd3/radscheduleview_recurrencerule.png)
