Outlook Contacts: Create a Contact
See more Outlook Contact Examples
Create a contact in the default Contacts folder using delegated Contacts.ReadWrite. The JSON demonstrates names, a display name, an email-address object, and a phone-number array. HTTP 201 confirms creation; save the returned string ID for later reads and updates. Creating a contact does not send a message or invitation.
Live creation: this adds a real mailbox contact. Contacts do not have the event resource's transactionId deduplication feature. The sample records a pending attempt before POST and a receipt after success. Rerunning a completed operation prints the saved ID; an uncertain outcome blocks an automatic repeat POST. Reconcile the contact list first, since a matching name or email does not prove uniqueness. Even a failed local receipt write does not undo server creation. Use a new operation filename only for a deliberately new contact; protect these files per account and use one writer. Production code needs durable operation storage.
To create in a specific folder, use /me/contactFolders/{folderId}/contacts. The folder ID is different from the new contact's ID. Read-only fields such as id, parentFolderId, and changeKey are not included in the request.
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.
Keep contact and folder IDs as case-sensitive strings, together with their mailbox context. These examples request Prefer: IdType="ImmutableId" consistently; such contact IDs remain stable when moved within the same mailbox. Older stored default-format IDs need an intentional migration or fresh retrieval, not string manipulation. URL-encode each ID as a path segment. Display names and email addresses are not unique IDs.
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.
Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Token
oleobject loo_Http
oleobject loo_Resp
oleobject loo_Result
oleobject loo_Body
oleobject loo_Fac
string ls_OperationFile
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
loo_Http.SetRequestHeader("Prefer","IdType=~"ImmutableId~"")
// Live creation in the default Contacts folder. This does not send an email or invitation.
loo_Body = create oleobject
li_rc = loo_Body.ConnectToNewObject("Chilkat.JsonObject")
loo_Body.UpdateString("givenName","Alex")
loo_Body.UpdateString("surname","Example")
loo_Body.UpdateString("displayName","Alex Example")
loo_Body.UpdateString("emailAddresses[0].address","alex@example.com")
loo_Body.UpdateString("emailAddresses[0].name","Alex Example")
loo_Body.UpdateString("businessPhones[0]","+1 202 555 0100")
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_Fac
return
end if
ls_OperationFile = "qa_data/outlookContacts/create-contact.json"
loo_Operation = create oleobject
li_rc = loo_Operation.ConnectToNewObject("Chilkat.JsonObject")
loo_Operation.Strict = 1
if loo_Fac.FileExists(ls_OperationFile) = 1 then
li_Success = loo_Operation.LoadFile(ls_OperationFile)
if li_Success <> 1 then
Write-Debug "Could not read the previous operation. Reconcile before another attempt."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
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_Fac
destroy loo_Operation
return
end if
Write-Debug "Already created contact ID: " + loo_Operation.StringOf("id")
else
Write-Debug "A previous creation may have succeeded. Inspect the contact list and reconcile before retrying."
end if
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_Fac
destroy loo_Operation
return
end if
// Contacts have no event-style transactionId. A pending record prevents blind POST retries.
loo_Operation.UpdateString("status","pending")
loo_Operation.UpdateString("displayName",loo_Body.StringOf("displayName"))
loo_Operation.UpdateString("emailAddress",loo_Body.StringOf("emailAddresses[0].address"))
li_Success = loo_Operation.WriteFile(ls_OperationFile)
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_Fac
destroy loo_Operation
return
end if
li_Success = loo_Http.HttpJson("POST","https://graph.microsoft.com/v1.0/me/contacts",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_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_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_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_Fac
destroy loo_Operation
return
end if
loo_Operation.UpdateString("id",loo_Result.StringOf("id"))
loo_Operation.UpdateString("status","created")
// The contact exists even if saving this receipt fails. Keep this ID for reconciliation.
Write-Debug "Created contact ID: " + loo_Result.StringOf("id")
li_Success = loo_Operation.WriteFile(ls_OperationFile)
if li_Success <> 1 then
Write-Debug "Contact created, but the receipt could not be saved. Reconcile the pending attempt before retrying."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_Fac
destroy loo_Operation
return
end if
Write-Debug "Contact receipt saved."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_Fac
destroy loo_Operation