Search code examples
c#iosicloudicalendarcaldav

CALDAV Edit/Delete multiple events in ICLOUD


I have implemented caldav api to add ,update ,delete events in icloud calendars and everything is working as expected. When it comes to add,update,delete multiple events in one api call, the add works without much issues.But not sure how I can edit and delete multiple events in one api call.

Below code will give an idea of how I delete one event

public void DeleteEvent(CalendarToken authToken, string eventId, Action<APIResponse> callback)
    {
        var appleToken = GetAppleAuthToken(authToken);
        string url = @"https://" + appleToken.AppleCalendarDomainUrl + "/" + appleToken.AppleUserID + "/calendars/home/" + eventId + "_event.ics";
        string response = SendRequest(appleToken, url, string.Empty, "DELETE", "application/xml; charset=utf-8", "0");

    }
private string SendRequest(AppleCalendarToken appleToken, string destinationUrl, string requestData, string methodType, string contentType, string depthValue)
    {
        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
            byte[] bytes;
            bytes = System.Text.Encoding.ASCII.GetBytes(requestData);
            request.Credentials = GetCredentials(appleToken, destinationUrl);
            request.PreAuthenticate = true;
            request.ContentType = contentType; //"application/xml; encoding='utf-8'";
            request.ContentLength = bytes.Length;
            request.Method = methodType;
            request.Headers.Add("Depth", depthValue);
            request.Accept = "*/*";
            request.UserAgent = "cURL based CalDAV client";
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(bytes, 0, bytes.Length);
            requestStream.Close();
            HttpWebResponse response;
            response = (HttpWebResponse)request.GetResponse();
            if ((int)response.StatusCode == 207 || (int)response.StatusCode == 201)
            {
                Stream responseStream = response.GetResponseStream();
                return new StreamReader(responseStream).ReadToEnd();

            }
        }
        catch
        {
            throw;
        }

        return string.Empty;
    }

Below screenshot will show how I add multiple events in one api call enter image description here I am looking edit/delete more than one event in one api call. Any help would be appreciated. Thanks in advance.


Solution

  • The answer to that question is unfortunately really short: There is no bulk delete facility in WebDAV/CalDAV. You need to delete them one-by-one.

    (You can delete a whole calendar collection with a single DELETE, but thats probably not what you want.)

    Update/Clarification: Standard CalDAV/WebDAV does not support any bulk change operations. You can send multiple HTTP requests to the server at the same time using multiple connections, HTTP/2 multiplexing or HTTP/1.1 pipelining. And if the server is smart it can coalesce such changes. At least w/ HTTP/2 this arguably eliminates the need for a BATCH operation.

    There are two non-standard ways to perform bulk-changes:

    • a) A POST of a single vCalendar entity containing multiple events to the collection URL
    • b) the calendarserver-bulk-change draft which is supported by Apple Calendar Server and iCloud, maybe others

    The POST (sometimes even a PUT) to the collection in "a)" allows you to add and sometimes update (by using a matching UID) multiple events. Many servers actually support this in one way or the other. I recommend against using this, because the semantics are very unclear/not-standardized. For example, what happens if one sub-PUT fails etc.

    The Bulk-Change draft describes a POST for bulk changes, but is (AFAIK) not widely implemented. Nor did it ever become a RFC (and because w/ HTTP/2 it is kinda superfluous, I don't expect that to happen).