Sample code for 30+ languages & platforms
Ruby Requires Chilkat v11.6.0+

Outlook Calendar: Create, Rename, or Delete a Secondary Calendar

See more Outlook Calendar Examples

Select one operation: create (the default), rename, or delete. Creation returns an opaque calendar ID to save with the application record. To rename or delete, explicitly set that ID and change action. The example retrieves the calendar first and refuses to operate on the default calendar. Names are display labels and are not unique identifiers.

Live changes: rename changes the calendar name; delete removes the secondary calendar and its events. The operations are alternatives, not a create/rename/delete sequence. Never delete a calendar merely to clean up an example without checking its contents and ownership. Deletion succeeds with HTTP 204 and no JSON body.

Unlike event creation, calendar creation has no transactionId deduplication property. A private pending record is saved before sending; an uncertain outcome or failed receipt write blocks automatic repeat creation. Inspect the calendar list and reconcile that attempt first. Keep operation files per account, with a single writer. Use a new filename only for a deliberately new calendar. A completed receipt prevents accidental creation when rerunning the sample.

Delegated permission: Calendars.ReadWrite.

Uses Microsoft Graph v1.0 in the global cloud with a delegated access token saved by the calendar OAuth2 example. Both personal Outlook.com accounts and Microsoft 365 work/school accounts with a mailbox can use this flow. The app registration must support the selected account type. A token for SMTP/IMAP/POP or another API is not a Graph token.

/me means the signed-in user and requires delegated authentication. An unattended Microsoft 365 service instead needs application permissions and administrator consent, and addresses an explicit mailbox through /users/{id-or-userPrincipalName}. That client-credentials flow does not apply to personal Outlook.com accounts. These samples target the global cloud; national clouds use different authority and Graph hosts.

HTTP methods can return false for an HTTP error as well as a connection failure. The code distinguishes those cases and checks the expected status before reading JSON. For 401, refresh the access token or authorize again; for 403, check permissions, mailbox access, and tenant policy. For 429, honor Retry-After before a bounded retry. Do not automatically repeat a write after an uncertain outcome. Keep detailed errors and calendar data private.

Microsoft documentation

Chilkat Ruby Downloads

Ruby
require 'chilkat'

success = false

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false
#  Obtain a delegated Microsoft Graph token using the calendar authorization example.
#  See Authorize Outlook Calendar with OAuth2 and PKCE.
token = Chilkat::CkJsonObject.new()
token.put_Strict(true)
success = token.LoadFile("qa_data/tokens/outlookCalendar.json")
if (success != true)
    print "Could not read the private calendar token cache." + "\n";
    exit
end

if ((token.JsonTypeOf("access_token") != 1) or (token.StringOfEquals("access_token","",true) == true))
    print "Expected a nonempty access_token in the JSON." + "\n";
    exit
end

http = Chilkat::CkHttp.new()
http.put_AuthToken(token.stringOf("access_token"))
http.put_RequireSslCertVerify(true)
http.put_FollowRedirects(false)
http.put_ConnectTimeout(20)
http.put_ReadTimeout(30)
http.SetRequestHeader("Accept","application/json")
resp = Chilkat::CkHttpResponse.new()
result = Chilkat::CkJsonObject.new()
result.put_Strict(true)
action = "create"#  Choose create, rename, or delete.
calendarId = "YOUR_SECONDARY_CALENDAR_ID"
if ((action != "create") and (action != "rename") and (action != "delete"))
    print "Choose create, rename, or delete." + "\n";
    exit
end

body = Chilkat::CkJsonObject.new()
fac = Chilkat::CkFileAccess.new()
if (action == "create")
    success = fac.DirEnsureExists("qa_data/outlookCalendar")
    if (success != true)
        print "Could not create the private operation directory." + "\n";
        exit
    end

    receiptPath = "qa_data/outlookCalendar/secondary-calendar.json"
    operation = Chilkat::CkJsonObject.new()
    operation.put_Strict(true)
    if (fac.FileExists(receiptPath) == true)
        success = operation.LoadFile(receiptPath)
        if (success != true)
            print "Could not read the previous operation; inspect it before retrying." + "\n";
            exit
        end

        if (operation.StringOfEquals("status","created",true) == true)
            if ((operation.JsonTypeOf("id") != 1) or (operation.StringOfEquals("id","",true) == true))
                print "Expected a nonempty id in the JSON." + "\n";
                exit
            end

            print "Already created calendar ID: " + operation.stringOf("id") + "\n";
        else
            print "Creation may already have occurred. Reconcile the calendar list before starting another operation." + "\n";
        end

        exit
    end

    body.UpdateString("name","Secondary calendar")
    operation.UpdateString("status","pending")
    operation.UpdateString("name",body.stringOf("name"))
    #  Calendar creation has no event transactionId. Record an attempt before sending.
    success = operation.WriteFile(receiptPath)
    if (success != true)
        print "Could not record the creation attempt; no request sent." + "\n";
        exit
    end

    success = http.HttpJson("POST","https://graph.microsoft.com/v1.0/me/calendars",body,"application/json",resp)
    #  A false result can still include an HTTP error response.
    if ((success != true) and (resp.get_StatusCode() == 0))
        print "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying." + "\n";
        exit
    end

    if (resp.get_StatusCode() != 201)
        print "Unexpected HTTP status: " + resp.get_StatusCode().to_s() + "\n";
        print "Request ID: " + resp.getHeaderField("request-id") + "\n";
        if (resp.get_StatusCode() == 429)
            print "Retry-After: " + resp.getHeaderField("Retry-After") + "\n";
        end

        if (resp.get_StatusCode() == 401)
            print "Refresh the Graph access token or authorize again." + "\n";
        end

        exit
    end

    success = result.Load(resp.bodyStr())
    if (success != true)
        print "Expected a well-formed JSON response." + "\n";
        exit
    end

    if ((result.JsonTypeOf("id") != 1) or (result.StringOfEquals("id","",true) == true))
        print "Expected a nonempty id in the JSON." + "\n";
        exit
    end

    operation.UpdateString("id",result.stringOf("id"))
    operation.UpdateString("status","created")
    success = operation.WriteFile(receiptPath)
    if (success != true)
        print "Could not save the receipt. Reconcile the pending attempt before retrying." + "\n";
        exit
    end

    print "Created calendar ID: " + result.stringOf("id") + "\n";
    exit
end

#  Use the saved ID for a deliberately selected secondary calendar.
endpoint = Chilkat::CkStringBuilder.new()
endpoint.Append("https://graph.microsoft.com/v1.0/me/calendars/")
endpoint.Append(http.urlEncode(calendarId))
success = http.HttpNoBody("GET",endpoint.getAsString(),resp)
#  A false result can still include an HTTP error response.
if ((success != true) and (resp.get_StatusCode() == 0))
    print "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying." + "\n";
    exit
end

if (resp.get_StatusCode() != 200)
    print "Unexpected HTTP status: " + resp.get_StatusCode().to_s() + "\n";
    print "Request ID: " + resp.getHeaderField("request-id") + "\n";
    if (resp.get_StatusCode() == 429)
        print "Retry-After: " + resp.getHeaderField("Retry-After") + "\n";
    end

    if (resp.get_StatusCode() == 401)
        print "Refresh the Graph access token or authorize again." + "\n";
    end

    exit
end

success = result.Load(resp.bodyStr())
if (success != true)
    print "Expected a well-formed JSON response." + "\n";
    exit
end

if ((result.JsonTypeOf("id") != 1) or (result.StringOfEquals("id","",true) == true))
    print "Expected a nonempty id in the JSON." + "\n";
    exit
end

if (result.JsonTypeOf("isDefaultCalendar") != 5)
    print "Expected isDefaultCalendar in the calendar response." + "\n";
    exit
end

if (result.BoolOf("isDefaultCalendar") == true)
    print "This example operates only on secondary calendars." + "\n";
    exit
end

if (action == "rename")
    body.UpdateString("name","Renamed calendar")
    success = http.HttpJson("PATCH",endpoint.getAsString(),body,"application/json",resp)
    #  A false result can still include an HTTP error response.
    if ((success != true) and (resp.get_StatusCode() == 0))
        print "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying." + "\n";
        exit
    end

    if (resp.get_StatusCode() != 200)
        print "Unexpected HTTP status: " + resp.get_StatusCode().to_s() + "\n";
        print "Request ID: " + resp.getHeaderField("request-id") + "\n";
        if (resp.get_StatusCode() == 429)
            print "Retry-After: " + resp.getHeaderField("Retry-After") + "\n";
        end

        if (resp.get_StatusCode() == 401)
            print "Refresh the Graph access token or authorize again." + "\n";
        end

        exit
    end

    success = result.Load(resp.bodyStr())
    if (success != true)
        print "Expected a well-formed JSON response." + "\n";
        exit
    end

    if ((result.JsonTypeOf("id") != 1) or (result.StringOfEquals("id","",true) == true))
        print "Expected a nonempty id in the JSON." + "\n";
        exit
    end

    print "Renamed calendar ID: " + result.stringOf("id") + "\n";
else
    #  Live deletion removes this secondary calendar and its events.
    success = http.HttpNoBody("DELETE",endpoint.getAsString(),resp)
    #  A false result can still include an HTTP error response.
    if ((success != true) and (resp.get_StatusCode() == 0))
        print "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying." + "\n";
        exit
    end

    if (resp.get_StatusCode() != 204)
        print "Unexpected HTTP status: " + resp.get_StatusCode().to_s() + "\n";
        print "Request ID: " + resp.getHeaderField("request-id") + "\n";
        if (resp.get_StatusCode() == 429)
            print "Retry-After: " + resp.getHeaderField("Retry-After") + "\n";
        end

        if (resp.get_StatusCode() == 401)
            print "Refresh the Graph access token or authorize again." + "\n";
        end

        exit
    end

    print "Secondary calendar deleted (HTTP 204)." + "\n";
end