Search code examples
apihttprequestcaldavsabredav

Create a new calendar from CalDav API


I am trying to create a new calendar in my principal's home folder using SabreDav.

I couldn't find how to achieve this - is it even possible ?

UPDATE: I found out about MKCALENDAR method, but the following returns an "abandoned" request error:

<C:mkcalendar xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav"  xmlns:ical="http://apple.com/ns/ical/" >"
    <D:set>
        <D:prop>
            <D:displayname>cal Display Name</D:displayname> 
            <ical:calendar-color>cal Color</ical:calendar-color>
        </D:prop>
    </D:set>
</C:mkcalendar>

Sending it with a HttpWebRequest fails with a canceled request messgage...

Thanks in advance!

UPDATE 2: Some more details:

HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create("http://my.sabredavcalendar.srv/calendarserver.php/calendars/admin/my_new_calendar/");
Request.Method = "MKCALENDAR";
Request..Credentials = new NetworkCredentials("usr", "pwd");
Request.ContentType = "application/xml";
string body = "<C:mkcalendar [.....]  </C:mkcalendar>";
Request.ContentLength = body.Length;
// ---
// The using block throws an error...
using (Stream reqStream = Request.GetRequestStream()) {
    byte[] encodedBody = Encoding.UTF8.GetBytes(body);
    reqStream.Write(encodedBody, 0, encodedBody.Length);
    reqStream.Close();
}
Response = (HttpWebResponse)Request.GetResponse();

The error message I get is

The request was aborted: The request was canceled

On the server side, here is the access log:

192.168.1.200 - - [06/Jul/2015:09:51:48 +0200] "MKCALENDAR /calendarserver.php/calendars/admin/my_new_calendar/ HTTP/1.1" 400 25 "-" "-"

The error log is empty... so it seems I get a "bad request" response which is not caught when preparing the request?!

UPDATE 3: the body contains special characters as "éàê..." which is why the contentlength part was wrong !


Solution

  • I take hnh's comment as an answer: the problem was indeed the Request.ContentLength = body.Length.

    Corrected code is:

    Request.ContentLength = Encoding.UTF8.GetByteCount(body);