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

Visual FoxPro
LOCAL lnSuccess
LOCAL loToken
LOCAL loHttp
LOCAL loResp
LOCAL loResult
LOCAL lcAction
LOCAL lcFolderId
LOCAL lcParentFolderId
LOCAL loBody
LOCAL loEndpoint
LOCAL loFac
LOCAL lcReceiptPath
LOCAL loOperation

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0
*  Obtain a delegated Microsoft Graph token using the contacts authorization example.
*  See Authorize Outlook Contacts with OAuth2 and PKCE.
loToken = CreateObject('Chilkat.JsonObject')
loToken.Strict = 1
lnSuccess = loToken.LoadFile("qa_data/tokens/outlookContacts.json")
IF (lnSuccess <> 1) THEN
    ? "Could not read the private contacts token cache."
    RELEASE loToken
    CANCEL
ENDIF

IF ((loToken.JsonTypeOf("access_token") <> 1) OR (loToken.StringOfEquals("access_token","",1) = 1)) THEN
    ? "Expected a nonempty access_token in the JSON."
    RELEASE loToken
    CANCEL
ENDIF

loHttp = CreateObject('Chilkat.Http')
loHttp.AuthToken = loToken.StringOf("access_token")
loHttp.RequireSslCertVerify = 1
loHttp.FollowRedirects = 0
loHttp.ConnectTimeout = 20
loHttp.ReadTimeout = 30
loHttp.SetRequestHeader("Accept","application/json")
loResp = CreateObject('Chilkat.HttpResponse')
loResult = CreateObject('Chilkat.JsonObject')
loResult.Strict = 1
lcAction = "create"*  Choose create, rename, or delete.
lcFolderId = "YOUR_CUSTOM_CONTACT_FOLDER_ID"
lcParentFolderId = ""*  Optional parent for creating a child folder.
IF ((lcAction <> "create") AND (lcAction <> "rename") AND (lcAction <> "delete")) THEN
    ? "Choose create, rename, or delete."
    RELEASE loToken
    RELEASE loHttp
    RELEASE loResp
    RELEASE loResult
    CANCEL
ENDIF

loBody = CreateObject('Chilkat.JsonObject')
loEndpoint = CreateObject('Chilkat.StringBuilder')
IF (lcAction = "create") THEN
    loFac = CreateObject('Chilkat.FileAccess')
    lnSuccess = loFac.DirEnsureExists("qa_data/outlookContacts")
    IF (lnSuccess <> 1) THEN
        ? "Could not create the private operation directory."
        RELEASE loToken
        RELEASE loHttp
        RELEASE loResp
        RELEASE loResult
        RELEASE loBody
        RELEASE loEndpoint
        RELEASE loFac
        CANCEL
    ENDIF

    lcReceiptPath = "qa_data/outlookContacts/create-folder.json"
    loOperation = CreateObject('Chilkat.JsonObject')
    loOperation.Strict = 1
    IF (loFac.FileExists(lcReceiptPath) = 1) THEN
        lnSuccess = loOperation.LoadFile(lcReceiptPath)
        IF (lnSuccess <> 1) THEN
            ? "Could not read the previous operation; reconcile before retrying."
            RELEASE loToken
            RELEASE loHttp
            RELEASE loResp
            RELEASE loResult
            RELEASE loBody
            RELEASE loEndpoint
            RELEASE loFac
            RELEASE loOperation
            CANCEL
        ENDIF

        IF (loOperation.StringOfEquals("status","created",1) = 1) THEN
            IF ((loOperation.JsonTypeOf("id") <> 1) OR (loOperation.StringOfEquals("id","",1) = 1)) THEN
                ? "Expected a nonempty id in the JSON."
                RELEASE loToken
                RELEASE loHttp
                RELEASE loResp
                RELEASE loResult
                RELEASE loBody
                RELEASE loEndpoint
                RELEASE loFac
                RELEASE loOperation
                CANCEL
            ENDIF

            ? "Already created folder ID: " + loOperation.StringOf("id")
        ELSE
            ? "A previous folder creation may have succeeded. Reconcile the folder list before retrying."
        ENDIF

        RELEASE loToken
        RELEASE loHttp
        RELEASE loResp
        RELEASE loResult
        RELEASE loBody
        RELEASE loEndpoint
        RELEASE loFac
        RELEASE loOperation
        CANCEL
    ENDIF

    loEndpoint.Append("https://graph.microsoft.com/v1.0/me/contactFolders")
    IF (lcParentFolderId <> "") THEN
        loEndpoint.Append("/")
        loEndpoint.Append(loHttp.UrlEncode(lcParentFolderId))
        loEndpoint.Append("/childFolders")
    ENDIF

    loBody.UpdateString("displayName","Additional contacts")
    loOperation.UpdateString("status","pending")
    loOperation.UpdateString("displayName",loBody.StringOf("displayName"))
    loOperation.UpdateString("parentFolderId",lcParentFolderId)
    lnSuccess = loOperation.WriteFile(lcReceiptPath)
    IF (lnSuccess <> 1) THEN
        ? "Could not record the attempt; no request sent."
        RELEASE loToken
        RELEASE loHttp
        RELEASE loResp
        RELEASE loResult
        RELEASE loBody
        RELEASE loEndpoint
        RELEASE loFac
        RELEASE loOperation
        CANCEL
    ENDIF

    lnSuccess = loHttp.HttpJson("POST",loEndpoint.GetAsString(),loBody,"application/json",loResp)
    *  A false result can still include an HTTP error response.
    IF ((lnSuccess <> 1) AND (loResp.StatusCode = 0)) THEN
        ? "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying."
        RELEASE loToken
        RELEASE loHttp
        RELEASE loResp
        RELEASE loResult
        RELEASE loBody
        RELEASE loEndpoint
        RELEASE loFac
        RELEASE loOperation
        CANCEL
    ENDIF

    IF (loResp.StatusCode <> 201) THEN
        ? "Unexpected HTTP status: " + STR(loResp.StatusCode)
        ? "Request ID: " + loResp.GetHeaderField("request-id")
        IF (loResp.StatusCode = 429) THEN
            ? "Retry-After: " + loResp.GetHeaderField("Retry-After")
        ENDIF

        IF (loResp.StatusCode = 401) THEN
            ? "Refresh the Graph access token or authorize again."
        ENDIF

        RELEASE loToken
        RELEASE loHttp
        RELEASE loResp
        RELEASE loResult
        RELEASE loBody
        RELEASE loEndpoint
        RELEASE loFac
        RELEASE loOperation
        CANCEL
    ENDIF

    lnSuccess = loResult.Load(loResp.BodyStr)
    IF (lnSuccess <> 1) THEN
        ? "Expected a well-formed JSON response."
        RELEASE loToken
        RELEASE loHttp
        RELEASE loResp
        RELEASE loResult
        RELEASE loBody
        RELEASE loEndpoint
        RELEASE loFac
        RELEASE loOperation
        CANCEL
    ENDIF

    IF ((loResult.JsonTypeOf("id") <> 1) OR (loResult.StringOfEquals("id","",1) = 1)) THEN
        ? "Expected a nonempty id in the JSON."
        RELEASE loToken
        RELEASE loHttp
        RELEASE loResp
        RELEASE loResult
        RELEASE loBody
        RELEASE loEndpoint
        RELEASE loFac
        RELEASE loOperation
        CANCEL
    ENDIF

    loOperation.UpdateString("id",loResult.StringOf("id"))
    loOperation.UpdateString("status","created")
    ? "Created folder ID: " + loResult.StringOf("id")
    lnSuccess = loOperation.WriteFile(lcReceiptPath)
    IF (lnSuccess <> 1) THEN
        ? "Folder created, but receipt save failed. Reconcile the pending attempt before retrying."
        RELEASE loToken
        RELEASE loHttp
        RELEASE loResp
        RELEASE loResult
        RELEASE loBody
        RELEASE loEndpoint
        RELEASE loFac
        RELEASE loOperation
        CANCEL
    ENDIF

    RELEASE loToken
    RELEASE loHttp
    RELEASE loResp
    RELEASE loResult
    RELEASE loBody
    RELEASE loEndpoint
    RELEASE loFac
    RELEASE loOperation
    CANCEL
ENDIF

*  Supply an explicitly selected custom folder ID. Do not target the default Contacts folder.
loEndpoint.Append("https://graph.microsoft.com/v1.0/me/contactFolders/")
loEndpoint.Append(loHttp.UrlEncode(lcFolderId))
lnSuccess = loHttp.HttpNoBody("GET",loEndpoint.GetAsString(),loResp)
*  A false result can still include an HTTP error response.
IF ((lnSuccess <> 1) AND (loResp.StatusCode = 0)) THEN
    ? "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying."
    RELEASE loToken
    RELEASE loHttp
    RELEASE loResp
    RELEASE loResult
    RELEASE loBody
    RELEASE loEndpoint
    RELEASE loFac
    RELEASE loOperation
    CANCEL
ENDIF

IF (loResp.StatusCode <> 200) THEN
    ? "Unexpected HTTP status: " + STR(loResp.StatusCode)
    ? "Request ID: " + loResp.GetHeaderField("request-id")
    IF (loResp.StatusCode = 429) THEN
        ? "Retry-After: " + loResp.GetHeaderField("Retry-After")
    ENDIF

    IF (loResp.StatusCode = 401) THEN
        ? "Refresh the Graph access token or authorize again."
    ENDIF

    RELEASE loToken
    RELEASE loHttp
    RELEASE loResp
    RELEASE loResult
    RELEASE loBody
    RELEASE loEndpoint
    RELEASE loFac
    RELEASE loOperation
    CANCEL
ENDIF

lnSuccess = loResult.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
    ? "Expected a well-formed JSON response."
    RELEASE loToken
    RELEASE loHttp
    RELEASE loResp
    RELEASE loResult
    RELEASE loBody
    RELEASE loEndpoint
    RELEASE loFac
    RELEASE loOperation
    CANCEL
ENDIF

IF ((loResult.JsonTypeOf("id") <> 1) OR (loResult.StringOfEquals("id","",1) = 1)) THEN
    ? "Expected a nonempty id in the JSON."
    RELEASE loToken
    RELEASE loHttp
    RELEASE loResp
    RELEASE loResult
    RELEASE loBody
    RELEASE loEndpoint
    RELEASE loFac
    RELEASE loOperation
    CANCEL
ENDIF

? "Selected folder: " + loResult.StringOf("displayName")
IF (lcAction = "rename") THEN
    loBody.UpdateString("displayName","Renamed contacts")
    lnSuccess = loHttp.HttpJson("PATCH",loEndpoint.GetAsString(),loBody,"application/json",loResp)
    *  A false result can still include an HTTP error response.
    IF ((lnSuccess <> 1) AND (loResp.StatusCode = 0)) THEN
        ? "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying."
        RELEASE loToken
        RELEASE loHttp
        RELEASE loResp
        RELEASE loResult
        RELEASE loBody
        RELEASE loEndpoint
        RELEASE loFac
        RELEASE loOperation
        CANCEL
    ENDIF

    IF (loResp.StatusCode <> 200) THEN
        ? "Unexpected HTTP status: " + STR(loResp.StatusCode)
        ? "Request ID: " + loResp.GetHeaderField("request-id")
        IF (loResp.StatusCode = 429) THEN
            ? "Retry-After: " + loResp.GetHeaderField("Retry-After")
        ENDIF

        IF (loResp.StatusCode = 401) THEN
            ? "Refresh the Graph access token or authorize again."
        ENDIF

        RELEASE loToken
        RELEASE loHttp
        RELEASE loResp
        RELEASE loResult
        RELEASE loBody
        RELEASE loEndpoint
        RELEASE loFac
        RELEASE loOperation
        CANCEL
    ENDIF

    lnSuccess = loResult.Load(loResp.BodyStr)
    IF (lnSuccess <> 1) THEN
        ? "Expected a well-formed JSON response."
        RELEASE loToken
        RELEASE loHttp
        RELEASE loResp
        RELEASE loResult
        RELEASE loBody
        RELEASE loEndpoint
        RELEASE loFac
        RELEASE loOperation
        CANCEL
    ENDIF

    IF ((loResult.JsonTypeOf("id") <> 1) OR (loResult.StringOfEquals("id","",1) = 1)) THEN
        ? "Expected a nonempty id in the JSON."
        RELEASE loToken
        RELEASE loHttp
        RELEASE loResp
        RELEASE loResult
        RELEASE loBody
        RELEASE loEndpoint
        RELEASE loFac
        RELEASE loOperation
        CANCEL
    ENDIF

    ? "Renamed folder ID: " + loResult.StringOf("id")
ELSE
    *  Live deletion also affects contacts and child folders contained here.
    lnSuccess = loHttp.HttpNoBody("DELETE",loEndpoint.GetAsString(),loResp)
    *  A false result can still include an HTTP error response.
    IF ((lnSuccess <> 1) AND (loResp.StatusCode = 0)) THEN
        ? "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying."
        RELEASE loToken
        RELEASE loHttp
        RELEASE loResp
        RELEASE loResult
        RELEASE loBody
        RELEASE loEndpoint
        RELEASE loFac
        RELEASE loOperation
        CANCEL
    ENDIF

    IF (loResp.StatusCode <> 204) THEN
        ? "Unexpected HTTP status: " + STR(loResp.StatusCode)
        ? "Request ID: " + loResp.GetHeaderField("request-id")
        IF (loResp.StatusCode = 429) THEN
            ? "Retry-After: " + loResp.GetHeaderField("Retry-After")
        ENDIF

        IF (loResp.StatusCode = 401) THEN
            ? "Refresh the Graph access token or authorize again."
        ENDIF

        RELEASE loToken
        RELEASE loHttp
        RELEASE loResp
        RELEASE loResult
        RELEASE loBody
        RELEASE loEndpoint
        RELEASE loFac
        RELEASE loOperation
        CANCEL
    ENDIF

    ? "Contact folder deleted (HTTP 204)."
ENDIF

RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loEndpoint
RELEASE loFac
RELEASE loOperation