.NET Server

ASP.NET CalDAV Server Example with Microsoft SQL Back-end, C#

In this article

ASP.NET CalDAV Server Example with Microsoft SQL Back-end, C#

Here we will describe how to install and configure CalDAVServer.SqlStorage C# example provided with SDK, that demonstrates how you can build a CalDAV Server with SQL back-end. This example runs in ASP.NET on IIS or IIS Express and stores all calendar data in Microsoft SQL Server database. It uses Basic authentication that validates credentials against a custom membership provider with credentials stored in web.config for the sake of simplicity. The CalDAVServer.SqlStorage example is a fully-functional CalDAV server that can be used to store events and to-dos (tasks). You can use it to access calendars and tasks using iOS Calendar, OS X Clalendar, Android CalDAV-Sync, Mozilla Lightning, eM Client or any other CalDAV client.

Prerequisites

To run this example you will need:

  • ASP.NET 4.5 or later on Windows Server 2012, Windows Server 2008, Windows 10, Windows 8, Windows 7 or Windows Vista.   
  • Microsoft SQL Server Express LocalDB or Microsoft SQL Express or Microsoft SQL Server 2014 / 2012 / 2008 / 2005.
       

Because this sample uses null-conditional operator you need Visual Studio 2015 to compile it. If you need to support Visual Studio 2013 or earlier use the 'ASP.NET CalDAV/CardDAV Server Application' wizard in Visual Studio 2013 or earlier to generate similar code that compiles in VS 2010-2013 and does not uses null-conditional operator.

Installation and Configuration

By default CalDAV Server example with SQL back-end is installed to C:\Users\<UserName>\Documents\IT Hit\WebDAV Server Engine\vX.X.X\Samples\CalDAVServer.SqlStorage\ folder.

Setting the License

To run the example, you will need a valid IT Hit WebDAV Server Engine License. You can download the license here. Note that the Engine is fully functional with a trial license and does not have any limitations. The trial license is valid for one month and the engine will stop working after this. You can check the expiration date inside the license file.

Place the license file in the root folder of the CalDAV Example (\Samples\CalDAVServer.SqlStorage\).

Configuring the Database

In case you have Microsoft SQL LocalDB installed and are running the sample from Visual Studio you do not need to configure the database. In this case your database file, WebDav.mdf, is located in \Samples\CalDAVServer.SqlStorage\App_Data\WebDav\DB\ and will be automatically attached when you run the sample from VS, see below:

CalDAV sample Microsoft SQL database default location.

If you have Microsoft SQL Express installed you need to update the connection string. The connection string for Microsoft SQL Express is provided in a web.config file.

If you do not have Microsoft SQL Express or LocalDB installed or running your sample in IIS you will need to create and configure the database. The database script, DB.sql is located in \Samples\CalDAVServer.SqlStorage\ folder. In most case, you will open the Microsoft SQL Server console and execute this script. The script will create the database with a name ‘WebDav’ and all required tables. It will also create a single calendar named 'Cal 1'. Finally update the connection string in the web.config file to access your new database.

The Project Classes

On the diagram below you can see the CalDAV classes in your project:

CalDAV classes diagram in CalDAVServer.SqlStorage sample.

On the next diagram you can see the ACL classes in your project:

ACL classes diagram in CalDAVServer.SqlStorage sample.

To adapt the sample to your needs, you will modify these classes to read and write data from and into your storage.

SQL Back-end

The database consists of entities depicted in the figure below.

CalDAV Server SQL Diagram

cal_Alarm Table

A reminder or alarm for an event or a to-do.

 Column NameData TypeAllow NullsDescription
  AlarmId uniqueidentifier No  
  EventComponentId uniqueidentifier No Component to which this alarm belongs.
  UID nvarchar No Event or to-do (calendar file) to which this alarm belongs. Only required here to reduce ammount of joins.
  Action nvarchar No Defines which action to be invoked when an alarm is triggered (AUDIO/DISPLAY/EMAIL).
  TriggerAbsoluteDateTimeUtc datetime2 Yes The absolute date-time for the alarm in UTC. NULL if relative time is specified.
  TriggerRelativeOffset bigint Yes The time for the trigger of the alarm relative to the start or the end of an event or to-do. NULL if absolute time is specified.
  TriggerRelatedStart bit Yes Relationship of the alarm trigger with respect to the start or end of the calendar component in case relative time is specified. NULL if absolute date-time is specified.
  Summary nvarchar Yes Text to be used as the message subject when the Action is EMAIL.
  Description nvarchar Yes Text to be displayed when the alarm is triggered if the Action is DISPLAY or the e-mail message body when the Action is EMAIL.
  Duration bigint Yes Alarm duration in ticks. 1 tick = 100 nanoseconds.
  Repeat int Yes The number of times the alarm should be repeated, after the initial trigger.

cal_Attachment Table

Contains event and to-do attchments.

 Column NameData TypeAllow NullsDescription
  AttachmentId uniqueidentifier No  
  EventComponentId uniqueidentifier No Component to which this attchment belongs.
  UID nvarchar No Event or to-do (calendar file) to which this attachment belongs. Only required here to reduce ammount of joins.
  MediaType nvarchar Yes Media type. Typically NULL when external URL is specified.
  ExternalUrl nvarchar Yes Attachment URL. NULL if file content is specified.
  Content varbinary Yes Attacment file content. NULL if external url is specified. It is recommended to keep attchment size below 256Kb. In case over 1Mb should be stored, convert to FILESTREAM, FileTable or store in file system.

cal_Attendee Table

Stores attendees for an event, to-do, journal and free-busy calendar components.

 Column NameData TypeAllow NullsDescription
  AttendeeId uniqueidentifier No  
  EventComponentId uniqueidentifier No Component to which this this attendee is assigned.
  UID nvarchar No Event or to-do (calendar file) to which this attendee belongs. Only required here to reduce ammount of joins.
  Email nvarchar Yes Attengee e-mail.
  CommonName nvarchar Yes Attendee common name.
  DirectoryEntryRef nvarchar Yes Reference to a directory entry associated with the attendee.
  Language nvarchar Yes Language.
  UserType nvarchar Yes Calendar user type (INDIVIDUAL/GROUP/RESOURCE/ROOM/UNKNOWN)
  SentBy nvarchar Yes Used to indicate whom is acting on behalf of the attendee. Typically e-mail.
  DelegatedFrom nvarchar Yes Used to indicate whom the request was delegated from. Typically e-mail.
  DelegatedTo nvarchar Yes Used to indicate the calendar users that the original request was delegated to. Typically e-mail.
  Rsvp bit Yes Used for indicating whether the favor of a reply is requested.
  ParticipationRole nvarchar Yes Used to specify the participation role (CHAIR/REQ-PARTICIPANT/OPT-PARTICIPANT/NON-PARTICIPANT).
  ParticipationStatus nvarchar Yes Used to specify participation status (NEEDS-ACTION/ACCEPTED/DECLINED/TENTATIVE/DELEGATED/COMPLETED/IN-PROCESS).

cal_CalendarFile Table

Contains calendar files (.ics). Each file contains one or more event or to-do instances.

 Column NameData TypeAllow NullsDescription
  UID nvarchar No Event or to-do (calendar file) UID. All events or to-dos components withing a file has this UID.
  CalendarFolderId uniqueidentifier No Calendar (calendar folder) to which this component belongs to.
  ETag timestamp No Automaticaly changes each time this object is updated. Used for synchronization operations.
  CreatedUtc datetime2 No Time when this file was created. Typically CalDAV clients never request this property, used for demo purposes only.
  ModifiedUtc datetime2 No Time when this file was modified. This property is updated to trigger ETag update. Typically CalDAV clients never request this property, used for demo purposes only.

cal_CalendarFolder Table

Contains calendars (calendar folders). Calendar folder contains calendar files each containing event or to-do description.

 Column NameData TypeAllow NullsDescription
  CalendarFolderId uniqueidentifier No  
  Name nvarchar No Calendar name.
  Description nvarchar No Calendar description.

cal_CalendarFolderProperty Table

Calendar WebDAV custom properties.

 Column NameData TypeAllow NullsDescription
  CalendarFolderPropertyId uniqueidentifier No  
  CalendarFolderId uniqueidentifier No Calendar (calendar folder) to which this custom property belongs to.
  Name nvarchar No Custom property name.
  Namespace nvarchar No Custom property namespace.
  PropVal nvarchar No Custom property value.

cal_RecurrenceException Table

Contains exceptions for recurring events and to-dos.

 Column NameData TypeAllow NullsDescription
  RecurrenceExceptionId uniqueidentifier No  
  EventComponentId uniqueidentifier No Component to which this this recurrence rule exception belongs.
  UID nvarchar No Event or to-do (calendar file) to which this recurrence exception belongs. Only required here to reduce ammount of joins.
  ExceptionDate datetime2 No Recurrence exception date.
  TimeZoneId nvarchar Yes Recurrence exception date time zone ID. If if NULL - ExceptionDate is a "floating" time. If contains "UTC" - the ExceptionDate is in UTC.
  AllDay bit No Time in ExceptionDate should be ignored for all-day events or to-dos.

cal_EventComponent Table

Stores calendars events and to-dos components (VEVENT and VTODO). Every calendar file can contain more than one component all sharing the same UID.

 Column NameData TypeAllow NullsDescription
  EventComponentId uniqueidentifier No  
  UID nvarchar No Event or to-do (calendar file) to which this component belongs to.
  ComponentType bit No Type of component. 1 - event, 0 - to-do.
  DateTimeStampUtc datetime2 No Specifies the date and time in UTC when the object was created.
  CreatedUtc datetime2 Yes Specifies the date and time in UTC when the calendar information was created by the calendar user agent in the calendar store.
  LastModifiedUtc datetime2 Yes Specifies the date and time in UTC when the information associated with the calendar component was last revised in the calendar store.
  Summary nvarchar Yes Defines a short summary or subject.
  Description nvarchar Yes Provides a more complete description of the calendar component than that provided by the "SUMMARY" property.
  OrganizerEmail nvarchar Yes Specifies e-mail of the organizer of the event or to-do.
  OrganizerCommonName nvarchar Yes Specifies common name of the organizer of the event or to-do.
  Start datetime2 Yes Specifies the inclusive start of the event or to-do. For recurring events and to-dos, it also specifies the very first recurrence instance. Could be "floating" time, time relative to time zone in StartTimeZone field or UTC.
  StartTimeZoneId nvarchar Yes Time zone ID for event or to-do Start time. If if NULL - Start is a "floating" time. If contains "UTC" - Start is in UTC.
  End datetime2 Yes Specifies the non-inclusive end of the event or due time for to-do component. Could be "floating" time, time relative to time zone in EndTimeZone field or UTC. NULL if Duration is specified.
  EndTimeZoneId nvarchar Yes Time zone ID for event End or to-do due time. If if NULL - End is a "floating" time. If contains "UTC" - the End is in UTC. Contains NULL if Duration is specified.
  Duration bigint Yes Event or to-do duration in ticks. 1 tick = 100 nanoseconds. NULL if End is specified.
  AllDay bit Yes True for all-day event or to-do. Time in Start and End/Due property should be ignored for all-day events or to-dos. StartTimeZone/EndTimeZone must be set to NULL for all-day events and to-dos.
  Class nvarchar Yes Specifies access classification (PUBLIC/PRIVATE/CONFIDENTIAL).
  Location nvarchar Yes Location description.
  Priority tinyint Yes Relative priority (0-9). A value of 0 specifies an undefined priority. A value of 1 is the highest priority.
  Sequence int Yes Defines the revision sequence number of the calendar component within a sequence of revisions.
  Status nvarchar Yes Overall status or confirmation for the event or to-do (TENTATIVE/CONFIRMED/CANCELLED).
  Categories nvarchar Yes Specifies categories or subtypes of the calendar component, coma-separated list.
  RecurFrequency nvarchar Yes Recurrence rule frequency (SECONDLY/MINUTELY/HOURLY/DAILY/WEEKLY/MONTHLY/YEARLY).
  RecurInterval int Yes Positive integer representing at which intervals the recurrence rule repeats.
  RecurUntil datetime2 Yes Date-time untill which the recurrence rule is valid. Could be "floating" time or UTC, depending on Start value.
  RecurCount int Yes Contains the number of occurrences at which to range-bound the recurrence.
  RecurWeekStart nvarchar Yes RecurWeekStart.
  RecurByDay nvarchar Yes Days of the week for weekly, monthly or yearly recurrence rule separated with ','. For example: 'TU,WE,FR' or '1SU,-1SU'.
  RecurByMonthDay nvarchar Yes Comma-separated list of days of the month. Valid values are 1 to 31 or -31 to -1.
  RecurByMonth nvarchar Yes Comma-separated list of months of the year. Valid values are 1 to 12.
  RecurBySetPos nvarchar Yes Comma-separated list of days of the month. Valid values are 1 to 31 or -31 to -1.
  RecurrenceIdDate datetime2 Yes Recurrence ID date or date and time. The value must be of the same type as Start value: "floating" time, UTC, or time in specific time zone.
  RecurrenceIdTimeZoneId nvarchar Yes Recurrence ID time zone ID.
  RecurrenceIdThisAndFuture bit Yes Recurrence ID RANGE parameter. If true - indicates a range defined by the given recurrence instance and all subsequent instances.
  EventTransparency bit Yes Defines whether or not an event is transparent to busy time searches. Valid for events only.
  ToDoCompletedUtc datetime2 Yes Defines the date and time in UTC that a to-do was actually completed. Valid for to-dos only.
  ToDoPercentComplete tinyint Yes Percent completion of a to-do. Valid for to-dos only.

cal_CustomProperty Table

Stores iCalendar custom properties and parameters.

 Column NameData TypeAllow NullsDescription
  CustomPropertyId uniqueidentifier No  
  ParentId uniqueidentifier No Parent component ID or parent property ID to which this custom property or parameter belongs to. This could be EventComponentId, AlarmId, AttachmentId, AttendeeId.
  UID nvarchar No Event or to-do (calendar file) to which this custom property or property parameter belongs to.
  PropertyName nvarchar No Property name. This could be a custom property name (starting with 'X-') or standard property name in case standard property contains custom parameters.
  ParameterName nvarchar Yes Parameter name. If null - Value field contains property value. Otherwise Value field contains parameter value.
  Value nvarchar No Property or parameter value. If ParameterName is null - this is a property value. If ParameterName is not null - this is a parameter value.

cal_Access Table

Stores user calendar access privileges.

 Column NameData TypeAllow NullsDescription
  AccessId uniqueidentifier No  
  CalendarFolderId uniqueidentifier No Calendar (calendar folder) for which user privileges are applied.
  UserId nvarchar No User ID of the user that has access to a calendar.
  Owner bit No Specifies if a user owns a calendar.
  Read bit No User has a read privilege on a calendar.
  Write bit No User has a write privilege on a calendar.

 

See Also:

Next Article:

ASP.NET CardDAV Server Example with Microsoft SQL Back-end, C#