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.
Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Token
oleobject loo_Http
oleobject loo_Resp
oleobject loo_Result
string ls_Action
string ls_FolderId
string ls_ParentFolderId
oleobject loo_Body
oleobject loo_Endpoint
oleobject loo_Fac
string ls_ReceiptPath
oleobject loo_Operation
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
li_Success = 0
// Obtain a delegated Microsoft Graph token using the contacts authorization example.
// See Authorize Outlook Contacts with OAuth2 and PKCE.
loo_Token = create oleobject
li_rc = loo_Token.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
destroy loo_Token
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Token.Strict = 1
li_Success = loo_Token.LoadFile("qa_data/tokens/outlookContacts.json")
if li_Success <> 1 then
Write-Debug "Could not read the private contacts token cache."
destroy loo_Token
return
end if
if (loo_Token.JsonTypeOf("access_token") <> 1) OR (loo_Token.StringOfEquals("access_token","",1) = 1) then
Write-Debug "Expected a nonempty access_token in the JSON."
destroy loo_Token
return
end if
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
loo_Http.AuthToken = loo_Token.StringOf("access_token")
loo_Http.RequireSslCertVerify = 1
loo_Http.FollowRedirects = 0
loo_Http.ConnectTimeout = 20
loo_Http.ReadTimeout = 30
loo_Http.SetRequestHeader("Accept","application/json")
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
loo_Result = create oleobject
li_rc = loo_Result.ConnectToNewObject("Chilkat.JsonObject")
loo_Result.Strict = 1
ls_Action = "create"// Choose create, rename, or delete.
ls_FolderId = "YOUR_CUSTOM_CONTACT_FOLDER_ID"
ls_ParentFolderId = ""// Optional parent for creating a child folder.
if (ls_Action <> "create") AND (ls_Action <> "rename") AND (ls_Action <> "delete") then
Write-Debug "Choose create, rename, or delete."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
return
end if
loo_Body = create oleobject
li_rc = loo_Body.ConnectToNewObject("Chilkat.JsonObject")
loo_Endpoint = create oleobject
li_rc = loo_Endpoint.ConnectToNewObject("Chilkat.StringBuilder")
if ls_Action = "create" then
loo_Fac = create oleobject
li_rc = loo_Fac.ConnectToNewObject("Chilkat.FileAccess")
li_Success = loo_Fac.DirEnsureExists("qa_data/outlookContacts")
if li_Success <> 1 then
Write-Debug "Could not create the private operation directory."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_Endpoint
destroy loo_Fac
return
end if
ls_ReceiptPath = "qa_data/outlookContacts/create-folder.json"
loo_Operation = create oleobject
li_rc = loo_Operation.ConnectToNewObject("Chilkat.JsonObject")
loo_Operation.Strict = 1
if loo_Fac.FileExists(ls_ReceiptPath) = 1 then
li_Success = loo_Operation.LoadFile(ls_ReceiptPath)
if li_Success <> 1 then
Write-Debug "Could not read the previous operation; reconcile before retrying."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_Endpoint
destroy loo_Fac
destroy loo_Operation
return
end if
if loo_Operation.StringOfEquals("status","created",1) = 1 then
if (loo_Operation.JsonTypeOf("id") <> 1) OR (loo_Operation.StringOfEquals("id","",1) = 1) then
Write-Debug "Expected a nonempty id in the JSON."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_Endpoint
destroy loo_Fac
destroy loo_Operation
return
end if
Write-Debug "Already created folder ID: " + loo_Operation.StringOf("id")
else
Write-Debug "A previous folder creation may have succeeded. Reconcile the folder list before retrying."
end if
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_Endpoint
destroy loo_Fac
destroy loo_Operation
return
end if
loo_Endpoint.Append("https://graph.microsoft.com/v1.0/me/contactFolders")
if ls_ParentFolderId <> "" then
loo_Endpoint.Append("/")
loo_Endpoint.Append(loo_Http.UrlEncode(ls_ParentFolderId))
loo_Endpoint.Append("/childFolders")
end if
loo_Body.UpdateString("displayName","Additional contacts")
loo_Operation.UpdateString("status","pending")
loo_Operation.UpdateString("displayName",loo_Body.StringOf("displayName"))
loo_Operation.UpdateString("parentFolderId",ls_ParentFolderId)
li_Success = loo_Operation.WriteFile(ls_ReceiptPath)
if li_Success <> 1 then
Write-Debug "Could not record the attempt; no request sent."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_Endpoint
destroy loo_Fac
destroy loo_Operation
return
end if
li_Success = loo_Http.HttpJson("POST",loo_Endpoint.GetAsString(),loo_Body,"application/json",loo_Resp)
// A false result can still include an HTTP error response.
if (li_Success <> 1) AND (loo_Resp.StatusCode = 0) then
Write-Debug "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_Endpoint
destroy loo_Fac
destroy loo_Operation
return
end if
if loo_Resp.StatusCode <> 201 then
Write-Debug "Unexpected HTTP status: " + string(loo_Resp.StatusCode)
Write-Debug "Request ID: " + loo_Resp.GetHeaderField("request-id")
if loo_Resp.StatusCode = 429 then
Write-Debug "Retry-After: " + loo_Resp.GetHeaderField("Retry-After")
end if
if loo_Resp.StatusCode = 401 then
Write-Debug "Refresh the Graph access token or authorize again."
end if
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_Endpoint
destroy loo_Fac
destroy loo_Operation
return
end if
li_Success = loo_Result.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
Write-Debug "Expected a well-formed JSON response."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_Endpoint
destroy loo_Fac
destroy loo_Operation
return
end if
if (loo_Result.JsonTypeOf("id") <> 1) OR (loo_Result.StringOfEquals("id","",1) = 1) then
Write-Debug "Expected a nonempty id in the JSON."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_Endpoint
destroy loo_Fac
destroy loo_Operation
return
end if
loo_Operation.UpdateString("id",loo_Result.StringOf("id"))
loo_Operation.UpdateString("status","created")
Write-Debug "Created folder ID: " + loo_Result.StringOf("id")
li_Success = loo_Operation.WriteFile(ls_ReceiptPath)
if li_Success <> 1 then
Write-Debug "Folder created, but receipt save failed. Reconcile the pending attempt before retrying."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_Endpoint
destroy loo_Fac
destroy loo_Operation
return
end if
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_Endpoint
destroy loo_Fac
destroy loo_Operation
return
end if
// Supply an explicitly selected custom folder ID. Do not target the default Contacts folder.
loo_Endpoint.Append("https://graph.microsoft.com/v1.0/me/contactFolders/")
loo_Endpoint.Append(loo_Http.UrlEncode(ls_FolderId))
li_Success = loo_Http.HttpNoBody("GET",loo_Endpoint.GetAsString(),loo_Resp)
// A false result can still include an HTTP error response.
if (li_Success <> 1) AND (loo_Resp.StatusCode = 0) then
Write-Debug "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_Endpoint
destroy loo_Fac
destroy loo_Operation
return
end if
if loo_Resp.StatusCode <> 200 then
Write-Debug "Unexpected HTTP status: " + string(loo_Resp.StatusCode)
Write-Debug "Request ID: " + loo_Resp.GetHeaderField("request-id")
if loo_Resp.StatusCode = 429 then
Write-Debug "Retry-After: " + loo_Resp.GetHeaderField("Retry-After")
end if
if loo_Resp.StatusCode = 401 then
Write-Debug "Refresh the Graph access token or authorize again."
end if
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_Endpoint
destroy loo_Fac
destroy loo_Operation
return
end if
li_Success = loo_Result.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
Write-Debug "Expected a well-formed JSON response."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_Endpoint
destroy loo_Fac
destroy loo_Operation
return
end if
if (loo_Result.JsonTypeOf("id") <> 1) OR (loo_Result.StringOfEquals("id","",1) = 1) then
Write-Debug "Expected a nonempty id in the JSON."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_Endpoint
destroy loo_Fac
destroy loo_Operation
return
end if
Write-Debug "Selected folder: " + loo_Result.StringOf("displayName")
if ls_Action = "rename" then
loo_Body.UpdateString("displayName","Renamed contacts")
li_Success = loo_Http.HttpJson("PATCH",loo_Endpoint.GetAsString(),loo_Body,"application/json",loo_Resp)
// A false result can still include an HTTP error response.
if (li_Success <> 1) AND (loo_Resp.StatusCode = 0) then
Write-Debug "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_Endpoint
destroy loo_Fac
destroy loo_Operation
return
end if
if loo_Resp.StatusCode <> 200 then
Write-Debug "Unexpected HTTP status: " + string(loo_Resp.StatusCode)
Write-Debug "Request ID: " + loo_Resp.GetHeaderField("request-id")
if loo_Resp.StatusCode = 429 then
Write-Debug "Retry-After: " + loo_Resp.GetHeaderField("Retry-After")
end if
if loo_Resp.StatusCode = 401 then
Write-Debug "Refresh the Graph access token or authorize again."
end if
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_Endpoint
destroy loo_Fac
destroy loo_Operation
return
end if
li_Success = loo_Result.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
Write-Debug "Expected a well-formed JSON response."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_Endpoint
destroy loo_Fac
destroy loo_Operation
return
end if
if (loo_Result.JsonTypeOf("id") <> 1) OR (loo_Result.StringOfEquals("id","",1) = 1) then
Write-Debug "Expected a nonempty id in the JSON."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_Endpoint
destroy loo_Fac
destroy loo_Operation
return
end if
Write-Debug "Renamed folder ID: " + loo_Result.StringOf("id")
else
// Live deletion also affects contacts and child folders contained here.
li_Success = loo_Http.HttpNoBody("DELETE",loo_Endpoint.GetAsString(),loo_Resp)
// A false result can still include an HTTP error response.
if (li_Success <> 1) AND (loo_Resp.StatusCode = 0) then
Write-Debug "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_Endpoint
destroy loo_Fac
destroy loo_Operation
return
end if
if loo_Resp.StatusCode <> 204 then
Write-Debug "Unexpected HTTP status: " + string(loo_Resp.StatusCode)
Write-Debug "Request ID: " + loo_Resp.GetHeaderField("request-id")
if loo_Resp.StatusCode = 429 then
Write-Debug "Retry-After: " + loo_Resp.GetHeaderField("Retry-After")
end if
if loo_Resp.StatusCode = 401 then
Write-Debug "Refresh the Graph access token or authorize again."
end if
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_Endpoint
destroy loo_Fac
destroy loo_Operation
return
end if
Write-Debug "Contact folder deleted (HTTP 204)."
end if
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_Endpoint
destroy loo_Fac
destroy loo_Operation