Sync Calendar Events using CALDAV in C#

In this post I will walk you through the steps of reading calendar events from CALDAV supported servers. Yahoo, Google, etc. supports CALDAV

What is CALDAV?

CALDAV is an internet standard allowing client to access scheduling information on remote server. It’s an extension of WebDAV which is a HTTP-based protocol for data manipulation. The protocol is defined by RFC 4791. As it’s an extension to WEBDAV protocol it uses HTTP verbs to manipulate calendar events some of them mostly used are PROPFIND, SEARCH, etc.

Background Information

  1. Every server has a separate URI to access calendar events.

Yahoo: https://caldav.calendar.yahoo.com/dav/user_name/Calendar/calendar_name/

Google: https://www.google.com/calendar/dav/user_name/events/

  1. Each Calendar folder is a collection and contains many calendar events. Each calendar event is a file and ended with .ics extension. .ics is well understood by many clients and you can easily export data from .ics to local client application.

How to Program

Now let’s see how to write a code to call Yahoo calendar events

  1. First of all create a new project in Microsoft Visual C# .NET
  2. Add reference to DDay.iCal library from Nuget Package manager console. This library will be used to read .ics files and read information about each event.
  3. Write below code to retrieve calendar event on any click event.

     


    In the above code snippet you have to configure your user name, password and modify the calendar URI depending on your calendarname and username.

If you see the code, I specified a content string which contains the request that I am sending it to the server. CALDAV have specific request formats which you can study here

ExecuteMethod is a helper method that sends request to the server and returns the response stream. Once I get the response stream I load the XML document and read the InnerXml of the file to get the complete XML. Then I parse the xml document and search for DAV:href element that contains the calendar event file (.ics) information. Once I get the list of .ics file paths, I call DownloadICS helper method to get the complete event information.

Helper Methods
Following are the helper method that performs specific operations. Please write these in your code to compile the project.

DownloadICS – Downloads .ics files


 

ExecuteMethod: Request calendar and returns response stream


9 thoughts on “Sync Calendar Events using CALDAV in C#

  1. We have a web application in which users will create appointments like hair cut or any thing else .

    And we want a new functionality to synchronize those appointments on Android , iphone and Outlook.

    Please let me know how CALDAV helps us to accomplish it .

    Any quick reply from you will be appreciated.

    Thanks in advance 🙂

  2. We have a web application in which users will create appointments like hair cut or any thing else .

    And we want a new functionality to synchronize those appointments on Android , iphone and Outlook.

    Please let me know how CALDAV helps us to accomplish it .
    Any quick reply from you will be appreciated.

    Thanks in advance 🙂

    • Sorry for replying late. Actually in my post I am consuming Yahoo web mail CALDAV services following CALDAV protocol. Yahoo have already exposed the CALDAV connection parameters to connect with their CALDAV server and retrieve items. If you are storing the Calendar Items in some custom database you have to work on the CALDAV server side as well and need to create a server which listen to the requests of CALDAV clients and follow the protocol.

    • Which server you are using to read CalDav events. Also, check the URI and maybe some change at the protocol on server side. Kindly check the server settings mostly they have given the specs to access CalDav resources.

Leave a comment