Sample code for 30+ languages & platforms
DataFlex 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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoToken
    Handle hoHttp
    Variant vResp
    Handle hoResp
    Handle hoResult
    String sAction
    String sCalendarId
    Variant vBody
    Handle hoBody
    Handle hoFac
    String sReceiptPath
    Handle hoOperation
    Handle hoEndpoint
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    Move False To iSuccess
    //  Obtain a delegated Microsoft Graph token using the calendar authorization example.
    //  See Authorize Outlook Calendar with OAuth2 and PKCE.
    Get Create (RefClass(cComChilkatJsonObject)) To hoToken
    If (Not(IsComObjectCreated(hoToken))) Begin
        Send CreateComObject of hoToken
    End
    Set ComStrict Of hoToken To True
    Get ComLoadFile Of hoToken "qa_data/tokens/outlookCalendar.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not read the private calendar token cache."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoToken "access_token" To iTemp1
    Get ComStringOfEquals Of hoToken "access_token" "" True To bTemp1
    If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
        Showln "Expected a nonempty access_token in the JSON."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Get ComStringOf Of hoToken "access_token" To sTemp1
    Set ComAuthToken Of hoHttp To sTemp1
    Set ComRequireSslCertVerify Of hoHttp To True
    Set ComFollowRedirects Of hoHttp To False
    Set ComConnectTimeout Of hoHttp To 20
    Set ComReadTimeout Of hoHttp To 30
    Send ComSetRequestHeader To hoHttp "Accept" "application/json"
    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoResult
    If (Not(IsComObjectCreated(hoResult))) Begin
        Send CreateComObject of hoResult
    End
    Set ComStrict Of hoResult To True
    Move "create" To sAction    //  Choose create, rename, or delete.
    Move "YOUR_SECONDARY_CALENDAR_ID" To sCalendarId
    If ((sAction <> "create") And (sAction <> "rename") And (sAction <> "delete")) Begin
        Showln "Choose create, rename, or delete."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoBody
    If (Not(IsComObjectCreated(hoBody))) Begin
        Send CreateComObject of hoBody
    End
    Get Create (RefClass(cComCkFileAccess)) To hoFac
    If (Not(IsComObjectCreated(hoFac))) Begin
        Send CreateComObject of hoFac
    End
    If (sAction = "create") Begin
        Get ComDirEnsureExists Of hoFac "qa_data/outlookCalendar" To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not create the private operation directory."
            Procedure_Return
        End

        Move "qa_data/outlookCalendar/secondary-calendar.json" To sReceiptPath
        Get Create (RefClass(cComChilkatJsonObject)) To hoOperation
        If (Not(IsComObjectCreated(hoOperation))) Begin
            Send CreateComObject of hoOperation
        End
        Set ComStrict Of hoOperation To True
        Get ComFileExists Of hoFac sReceiptPath To bTemp1
        If (bTemp1 = True) Begin
            Get ComLoadFile Of hoOperation sReceiptPath To iSuccess
            If (iSuccess <> True) Begin
                Showln "Could not read the previous operation; inspect it before retrying."
                Procedure_Return
            End

            Get ComStringOfEquals Of hoOperation "status" "created" True To bTemp1
            If (bTemp1 = True) Begin
                Get ComJsonTypeOf Of hoOperation "id" To iTemp1
                Get ComStringOfEquals Of hoOperation "id" "" True To bTemp1
                If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
                    Showln "Expected a nonempty id in the JSON."
                    Procedure_Return
                End

                Get ComStringOf Of hoOperation "id" To sTemp1
                Showln "Already created calendar ID: " sTemp1
            End
            Else Begin
                Showln "Creation may already have occurred. Reconcile the calendar list before starting another operation."
            End

            Procedure_Return
        End

        Get ComUpdateString Of hoBody "name" "Secondary calendar" To iSuccess
        Get ComUpdateString Of hoOperation "status" "pending" To iSuccess
        Get ComStringOf Of hoBody "name" To sTemp1
        Get ComUpdateString Of hoOperation "name" sTemp1 To iSuccess
        //  Calendar creation has no event transactionId. Record an attempt before sending.
        Get ComWriteFile Of hoOperation sReceiptPath To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not record the creation attempt; no request sent."
            Procedure_Return
        End

        Get pvComObject of hoBody to vBody
        Get pvComObject of hoResp to vResp
        Get ComHttpJson Of hoHttp "POST" "https://graph.microsoft.com/v1.0/me/calendars" vBody "application/json" vResp To iSuccess
        //  A false result can still include an HTTP error response.
        Get ComStatusCode Of hoResp To iTemp1
        If ((iSuccess <> True) And (iTemp1 = 0)) Begin
            Showln "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying."
            Procedure_Return
        End

        Get ComStatusCode Of hoResp To iTemp1
        If (iTemp1 <> 201) Begin
            Get ComStatusCode Of hoResp To iTemp1
            Showln "Unexpected HTTP status: " iTemp1
            Get ComGetHeaderField Of hoResp "request-id" To sTemp1
            Showln "Request ID: " sTemp1
            Get ComStatusCode Of hoResp To iTemp1
            If (iTemp1 = 429) Begin
                Get ComGetHeaderField Of hoResp "Retry-After" To sTemp1
                Showln "Retry-After: " sTemp1
            End

            Get ComStatusCode Of hoResp To iTemp1
            If (iTemp1 = 401) Begin
                Showln "Refresh the Graph access token or authorize again."
            End

            Procedure_Return
        End

        Get ComBodyStr Of hoResp To sTemp1
        Get ComLoad Of hoResult sTemp1 To iSuccess
        If (iSuccess <> True) Begin
            Showln "Expected a well-formed JSON response."
            Procedure_Return
        End

        Get ComJsonTypeOf Of hoResult "id" To iTemp1
        Get ComStringOfEquals Of hoResult "id" "" True To bTemp1
        If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
            Showln "Expected a nonempty id in the JSON."
            Procedure_Return
        End

        Get ComStringOf Of hoResult "id" To sTemp1
        Get ComUpdateString Of hoOperation "id" sTemp1 To iSuccess
        Get ComUpdateString Of hoOperation "status" "created" To iSuccess
        Get ComWriteFile Of hoOperation sReceiptPath To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not save the receipt. Reconcile the pending attempt before retrying."
            Procedure_Return
        End

        Get ComStringOf Of hoResult "id" To sTemp1
        Showln "Created calendar ID: " sTemp1
        Procedure_Return
    End

    //  Use the saved ID for a deliberately selected secondary calendar.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoEndpoint
    If (Not(IsComObjectCreated(hoEndpoint))) Begin
        Send CreateComObject of hoEndpoint
    End
    Get ComAppend Of hoEndpoint "https://graph.microsoft.com/v1.0/me/calendars/" To iSuccess
    Get ComUrlEncode Of hoHttp sCalendarId To sTemp1
    Get ComAppend Of hoEndpoint sTemp1 To iSuccess
    Get ComGetAsString Of hoEndpoint To sTemp1
    Get pvComObject of hoResp to vResp
    Get ComHttpNoBody Of hoHttp "GET" sTemp1 vResp To iSuccess
    //  A false result can still include an HTTP error response.
    Get ComStatusCode Of hoResp To iTemp1
    If ((iSuccess <> True) And (iTemp1 = 0)) Begin
        Showln "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying."
        Procedure_Return
    End

    Get ComStatusCode Of hoResp To iTemp1
    If (iTemp1 <> 200) Begin
        Get ComStatusCode Of hoResp To iTemp1
        Showln "Unexpected HTTP status: " iTemp1
        Get ComGetHeaderField Of hoResp "request-id" To sTemp1
        Showln "Request ID: " sTemp1
        Get ComStatusCode Of hoResp To iTemp1
        If (iTemp1 = 429) Begin
            Get ComGetHeaderField Of hoResp "Retry-After" To sTemp1
            Showln "Retry-After: " sTemp1
        End

        Get ComStatusCode Of hoResp To iTemp1
        If (iTemp1 = 401) Begin
            Showln "Refresh the Graph access token or authorize again."
        End

        Procedure_Return
    End

    Get ComBodyStr Of hoResp To sTemp1
    Get ComLoad Of hoResult sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Expected a well-formed JSON response."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoResult "id" To iTemp1
    Get ComStringOfEquals Of hoResult "id" "" True To bTemp1
    If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
        Showln "Expected a nonempty id in the JSON."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoResult "isDefaultCalendar" To iTemp1
    If (iTemp1 <> 5) Begin
        Showln "Expected isDefaultCalendar in the calendar response."
        Procedure_Return
    End

    Get ComBoolOf Of hoResult "isDefaultCalendar" To bTemp1
    If (bTemp1 = True) Begin
        Showln "This example operates only on secondary calendars."
        Procedure_Return
    End

    If (sAction = "rename") Begin
        Get ComUpdateString Of hoBody "name" "Renamed calendar" To iSuccess
        Get ComGetAsString Of hoEndpoint To sTemp1
        Get pvComObject of hoBody to vBody
        Get pvComObject of hoResp to vResp
        Get ComHttpJson Of hoHttp "PATCH" sTemp1 vBody "application/json" vResp To iSuccess
        //  A false result can still include an HTTP error response.
        Get ComStatusCode Of hoResp To iTemp1
        If ((iSuccess <> True) And (iTemp1 = 0)) Begin
            Showln "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying."
            Procedure_Return
        End

        Get ComStatusCode Of hoResp To iTemp1
        If (iTemp1 <> 200) Begin
            Get ComStatusCode Of hoResp To iTemp1
            Showln "Unexpected HTTP status: " iTemp1
            Get ComGetHeaderField Of hoResp "request-id" To sTemp1
            Showln "Request ID: " sTemp1
            Get ComStatusCode Of hoResp To iTemp1
            If (iTemp1 = 429) Begin
                Get ComGetHeaderField Of hoResp "Retry-After" To sTemp1
                Showln "Retry-After: " sTemp1
            End

            Get ComStatusCode Of hoResp To iTemp1
            If (iTemp1 = 401) Begin
                Showln "Refresh the Graph access token or authorize again."
            End

            Procedure_Return
        End

        Get ComBodyStr Of hoResp To sTemp1
        Get ComLoad Of hoResult sTemp1 To iSuccess
        If (iSuccess <> True) Begin
            Showln "Expected a well-formed JSON response."
            Procedure_Return
        End

        Get ComJsonTypeOf Of hoResult "id" To iTemp1
        Get ComStringOfEquals Of hoResult "id" "" True To bTemp1
        If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
            Showln "Expected a nonempty id in the JSON."
            Procedure_Return
        End

        Get ComStringOf Of hoResult "id" To sTemp1
        Showln "Renamed calendar ID: " sTemp1
    End
    Else Begin
        //  Live deletion removes this secondary calendar and its events.
        Get ComGetAsString Of hoEndpoint To sTemp1
        Get pvComObject of hoResp to vResp
        Get ComHttpNoBody Of hoHttp "DELETE" sTemp1 vResp To iSuccess
        //  A false result can still include an HTTP error response.
        Get ComStatusCode Of hoResp To iTemp1
        If ((iSuccess <> True) And (iTemp1 = 0)) Begin
            Showln "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying."
            Procedure_Return
        End

        Get ComStatusCode Of hoResp To iTemp1
        If (iTemp1 <> 204) Begin
            Get ComStatusCode Of hoResp To iTemp1
            Showln "Unexpected HTTP status: " iTemp1
            Get ComGetHeaderField Of hoResp "request-id" To sTemp1
            Showln "Request ID: " sTemp1
            Get ComStatusCode Of hoResp To iTemp1
            If (iTemp1 = 429) Begin
                Get ComGetHeaderField Of hoResp "Retry-After" To sTemp1
                Showln "Retry-After: " sTemp1
            End

            Get ComStatusCode Of hoResp To iTemp1
            If (iTemp1 = 401) Begin
                Showln "Refresh the Graph access token or authorize again."
            End

            Procedure_Return
        End

        Showln "Secondary calendar deleted (HTTP 204)."
    End



End_Procedure