C# example for CALDAV

admin

Administrator
Staff member
Can anyone please provide a CALDAV example for C#? Thanks in advance. I am struggling to find an example except this one.

<a href="https://ovaismehboob.wordpress.com/2014/03/30/sync-calendar-events-using-caldav/" rel="nofollow noreferrer">https://ovaismehboob.wordpress.com/2014/03/30/sync-calendar-events-using-caldav/</a>

But id does not work for me.
I get the following Error.

{"The remote server returned an error: (400) Bad Request."}

Here is the code,

Code:
public class Caldevx
{

    static System.IO.Stream ResponseStream;
    static System.Xml.XmlDocument XmlDocument;

    static string calendarURI = "https://caldav.calendar.yahoo.com/dav/";
    static string username = "[email protected]";
    static string password = "******";
    static string content = "&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;&lt;propfind xlmns\"DAV:\"&gt;&lt;allprop/&gt;&lt;/propfind&gt;";

    public void GetCalendars(){
        WebHeaderCollection whc = new WebHeaderCollection();
        whc.Add(@"Translate", "F");

        ResponseStream = ExectueMethod(username, password, calendarURI,"PROPFIND", whc, content, "text/xml");

        XmlDocument = new XmlDocument();
        XmlDocument.Load(ResponseStream);
        string xmlInner = XmlDocument.InnerXml;

        XmlDocument innerXmlDocument = new XmlDocument();
        innerXmlDocument.LoadXml(xmlInner);

        XmlNodeList lst = innerXmlDocument.GetElementsByTagName("DAV:href");
        List&lt;string&gt; lstICSs = new List&lt;string&gt;();

        foreach(XmlNode node in lst){
            if(node.InnerText.EndsWith(".ics"))
            {
                lstICSs.Add(node.InnerText.Trim());
            }

            ResponseStream.Close();

            if (lstICSs.Count &gt; 0)
            {
                DownloadICS("https//caldav.calendar.yahoo.com", lstICSs);
            }
        }
    }

    private void DownloadICS(string pathUri, List&lt;string&gt; fileNames)
    {
        WebClient request = new WebClient();
        request.Credentials = new NetworkCredential(username, password);

        foreach (var file in fileNames)
        {
            Byte[] data = request.DownloadData(pathUri + file);
            var str = System.Text.Encoding.Default.GetString(data);
            string path = @"C:\" + file.Substring(file.LastIndexOf("/")+1);

            FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
            fs.Write(data, 0, data.Length);
            fs.Close();

            DDay.iCal.IICalendarCollection calendars = iCalendar.LoadFromFile(path);

            foreach (var item in calendars)
            {
                foreach (Event e in item.Events)
                {
                    Console.WriteLine(e.Description);
                }
            }
        }
    }

    private Stream ExectueMethod(string username, string password, string caldevURI, string methodName, WebHeaderCollection headers, string content, string contentType)
    {
        HttpWebRequest httpGetRequest = (HttpWebRequest)WebRequest.Create(caldevURI);
        httpGetRequest.Credentials = new NetworkCredential(username, password);
        httpGetRequest.PreAuthenticate = true;
        httpGetRequest.Method = methodName;

        if (headers != null &amp;&amp; headers.HasKeys())
        {
            httpGetRequest.Headers = headers;
        }

        byte[] optionsArray = Encoding.UTF8.GetBytes(content);

        httpGetRequest.ContentLength = optionsArray.Length;

        if (!string.IsNullOrWhiteSpace(contentType))
        {
            httpGetRequest.ContentType = contentType;
        }

        Stream requestStream = httpGetRequest.GetRequestStream();
        requestStream.Write(optionsArray, 0, optionsArray.Length);
        requestStream.Close();

        HttpWebResponse httpGetResponse = (HttpWebResponse)httpGetRequest.GetResponse();
        Stream responseStream = httpGetResponse.GetResponseStream();

        return ResponseStream;
    }

}