Outlook Contacts: Download a Contact Photo
See more Outlook Contact Examples
Download the JPEG bytes at /me/contacts/{id}/photo/$value into a local file. This is the contact's photo, not the signed-in user's profile photo. The example checks status and Content-Type before saving, uses BinData for binary bytes, and does not decode the image as text or parse it as JSON.
A missing photo normally returns 404; an incorrect contact ID or mailbox can also cause 404, so verify the contact separately if needed. HTTP errors, an unexpected media type, and empty responses do not replace the existing file. A successful save replaces contact-photo.jpg; choose a separate private path per contact in an application. If supporting other image formats, validate the media type and use the appropriate filename extension.
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.
Delegated permission: Contacts.Read.
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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loToken
LOCAL loHttp
LOCAL loResp
LOCAL loResult
LOCAL lcContactId
LOCAL loEndpoint
LOCAL loMediaType
LOCAL lcContentType
LOCAL loImage
LOCAL loFac
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
loHttp.SetRequestHeader("Prefer",'IdType="ImmutableId"')
loHttp.SetRequestHeader("Accept","image/jpeg")
lcContactId = "YOUR_CONTACT_ID"
loEndpoint = CreateObject('Chilkat.StringBuilder')
loEndpoint.Append("https://graph.microsoft.com/v1.0/me/contacts/")
loEndpoint.Append(loHttp.UrlEncode(lcContactId))
loEndpoint.Append("/photo/$value")
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 loEndpoint
CANCEL
ENDIF
IF (loResp.StatusCode = 404) THEN
? "No contact photo was returned. Check the contact ID/mailbox if the contact itself is uncertain."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loEndpoint
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 loEndpoint
CANCEL
ENDIF
loMediaType = CreateObject('Chilkat.StringBuilder')
loMediaType.Append(loResp.GetHeaderField("Content-Type"))
lcContentType = loMediaType.GetBefore(";",0)
loMediaType.Clear()
loMediaType.Append(lcContentType)
loMediaType.Trim()
IF (loMediaType.ContentsEqual("image/jpeg",0) <> 1) THEN
? "Expected a JPEG response; existing photo file unchanged."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loEndpoint
RELEASE loMediaType
CANCEL
ENDIF
loImage = CreateObject('Chilkat.BinData')
lnSuccess = loResp.GetBodyBd(loImage)
IF (lnSuccess <> 1) THEN
? "Could not read the binary photo body."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loEndpoint
RELEASE loMediaType
RELEASE loImage
CANCEL
ENDIF
IF (loImage.NumBytes = 0) THEN
? "Empty image body; existing photo file unchanged."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loEndpoint
RELEASE loMediaType
RELEASE loImage
CANCEL
ENDIF
loFac = CreateObject('Chilkat.FileAccess')
lnSuccess = loFac.DirEnsureExists("qa_data/outlookContacts")
IF (lnSuccess <> 1) THEN
? "Could not create the photo directory."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loEndpoint
RELEASE loMediaType
RELEASE loImage
RELEASE loFac
CANCEL
ENDIF
lnSuccess = loImage.WriteFile("qa_data/outlookContacts/contact-photo.jpg")
IF (lnSuccess <> 1) THEN
? "Could not save the contact photo."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loEndpoint
RELEASE loMediaType
RELEASE loImage
RELEASE loFac
CANCEL
ENDIF
? "Saved qa_data/outlookContacts/contact-photo.jpg"
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loEndpoint
RELEASE loMediaType
RELEASE loImage
RELEASE loFac