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

Outlook Contacts: Create, Rename, or Delete a Contact Folder

See more Outlook Contact Examples

Choose one operation per run. Create (the default) posts a custom folder; an optional parentFolderId creates a child folder. Save the returned ID and use it explicitly for later rename or delete. These are alternatives, not an automatic create-and-delete sequence.

Live changes: rename changes the display name; deleting a folder affects its contacts and child folders. Inspect the selected custom folder and its contents before choosing delete. Do not target the default Contacts folder. The example reads the selected folder before changing it; this GET is not a confirmation dialog or a lock. The ordinary DELETE action returns HTTP 204 without JSON.

Creation records a pending attempt before POST, then saves the returned ID. There is no server idempotency key in this request. An uncertain result blocks a blind retry; reconcile the folder list first. Use private operation files per account and logical operation, and a single writer. Folder names are not unique.

Delegated permission: Contacts.ReadWrite.

Uses Microsoft Graph v1.0 for contacts stored in an Outlook.com or Microsoft 365 mailbox. These are personal mailbox contacts, not Microsoft Entra directory users, organizational contacts (/contacts), or the People API. Obtain a delegated token with the contacts OAuth2 example; the cache is qa_data/tokens/outlookContacts.json. A token for Calendar, SMTP, or another resource without the needed Graph contacts permission is insufficient.

/me requires a signed-in user. An authorized unattended Microsoft 365 application instead needs application permissions and administrator consent, and targets /users/{id-or-UPN}. App-only access does not apply to personal Outlook.com mailboxes. Shared folders require actual sharing rights and Contacts.Read.Shared or Contacts.ReadWrite.Shared delegated consent. The examples target the global cloud.

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 contact 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 sFolderId
    String sParentFolderId
    Variant vBody
    Handle hoBody
    Handle hoEndpoint
    Handle hoFac
    String sReceiptPath
    Handle hoOperation
    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 contacts authorization example.
    //  See Authorize Outlook Contacts 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/outlookContacts.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not read the private contacts 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_CUSTOM_CONTACT_FOLDER_ID" To sFolderId
    Move "" To sParentFolderId    //  Optional parent for creating a child folder.
    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(cComChilkatStringBuilder)) To hoEndpoint
    If (Not(IsComObjectCreated(hoEndpoint))) Begin
        Send CreateComObject of hoEndpoint
    End
    If (sAction = "create") Begin
        Get Create (RefClass(cComCkFileAccess)) To hoFac
        If (Not(IsComObjectCreated(hoFac))) Begin
            Send CreateComObject of hoFac
        End
        Get ComDirEnsureExists Of hoFac "qa_data/outlookContacts" To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not create the private operation directory."
            Procedure_Return
        End

        Move "qa_data/outlookContacts/create-folder.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; reconcile 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 folder ID: " sTemp1
            End
            Else Begin
                Showln "A previous folder creation may have succeeded. Reconcile the folder list before retrying."
            End

            Procedure_Return
        End

        Get ComAppend Of hoEndpoint "https://graph.microsoft.com/v1.0/me/contactFolders" To iSuccess
        If (sParentFolderId <> "") Begin
            Get ComAppend Of hoEndpoint "/" To iSuccess
            Get ComUrlEncode Of hoHttp sParentFolderId To sTemp1
            Get ComAppend Of hoEndpoint sTemp1 To iSuccess
            Get ComAppend Of hoEndpoint "/childFolders" To iSuccess
        End

        Get ComUpdateString Of hoBody "displayName" "Additional contacts" To iSuccess
        Get ComUpdateString Of hoOperation "status" "pending" To iSuccess
        Get ComStringOf Of hoBody "displayName" To sTemp1
        Get ComUpdateString Of hoOperation "displayName" sTemp1 To iSuccess
        Get ComUpdateString Of hoOperation "parentFolderId" sParentFolderId To iSuccess
        Get ComWriteFile Of hoOperation sReceiptPath To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not record the attempt; no request sent."
            Procedure_Return
        End

        Get ComGetAsString Of hoEndpoint To sTemp1
        Get pvComObject of hoBody to vBody
        Get pvComObject of hoResp to vResp
        Get ComHttpJson Of hoHttp "POST" 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 <> 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 ComStringOf Of hoResult "id" To sTemp1
        Showln "Created folder ID: " sTemp1
        Get ComWriteFile Of hoOperation sReceiptPath To iSuccess
        If (iSuccess <> True) Begin
            Showln "Folder created, but receipt save failed. Reconcile the pending attempt before retrying."
            Procedure_Return
        End

        Procedure_Return
    End

    //  Supply an explicitly selected custom folder ID. Do not target the default Contacts folder.
    Get ComAppend Of hoEndpoint "https://graph.microsoft.com/v1.0/me/contactFolders/" To iSuccess
    Get ComUrlEncode Of hoHttp sFolderId 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 ComStringOf Of hoResult "displayName" To sTemp1
    Showln "Selected folder: " sTemp1
    If (sAction = "rename") Begin
        Get ComUpdateString Of hoBody "displayName" "Renamed contacts" 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 folder ID: " sTemp1
    End
    Else Begin
        //  Live deletion also affects contacts and child folders contained here.
        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 "Contact folder deleted (HTTP 204)."
    End



End_Procedure