Outlook Calendar: Check User and Resource Free/Busy Availability
See more Outlook Calendar Examples
For Microsoft 365 organizational accounts, query users or resource mailboxes for the next eight hours using 30-minute slots. Replace the sample SMTP addresses. Delegated personal Microsoft accounts are not supported by getSchedule. Calendar access and tenant policy determine the details returned; successful HTTP status does not mean every schedule succeeded.
Check each schedule's error before reading its availability. Keep availabilityView as text: 0 is free or working elsewhere, 1 tentative, 2 busy, and 3 out of office. A free slot does not reserve a room or guarantee that it will remain free. Check the current getSchedule limits before batching larger address lists or time ranges, and split overly dense results if the service reports too many entries.
Delegated permission: Calendars.ReadBasic.
This example loads the delegated Graph token from the existing calendar authorization example. Use a work/school mailbox. An authorized app-only service can instead target /users/{id}/calendar/getSchedule.
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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loToken
LOCAL loHttp
LOCAL loResp
LOCAL loResult
LOCAL loBody
LOCAL loWhen
LOCAL i
LOCAL j
lnSuccess = 0
* Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0
* Obtain a delegated Microsoft Graph token using the calendar authorization example.
* See Authorize Outlook Calendar with OAuth2 and PKCE.
loToken = CreateObject('Chilkat.JsonObject')
loToken.Strict = 1
lnSuccess = loToken.LoadFile("qa_data/tokens/outlookCalendar.json")
IF (lnSuccess <> 1) THEN
? "Could not read the private calendar 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",'outlook.timezone="UTC", IdType="ImmutableId"')
loBody = CreateObject('Chilkat.JsonObject')
loBody.UpdateString("schedules[0]","user@example.com")
loBody.UpdateString("schedules[1]","room@example.com")
loBody.UpdateInt("availabilityViewInterval",30)
loWhen = CreateObject('Chilkat.CkDateTime')
lnSuccess = loWhen.SetFromCurrentSystemTime()
IF (lnSuccess <> 1) THEN
? "Could not get the window start."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loWhen
CANCEL
ENDIF
loBody.UpdateString("startTime.dateTime",loWhen.GetAsIso8601("YYYY-MM-DDThh:mm:ss",0))
loBody.UpdateString("startTime.timeZone","UTC")
lnSuccess = loWhen.AddSeconds(28800)
IF (lnSuccess <> 1) THEN
? "Could not set the eight-hour window."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loWhen
CANCEL
ENDIF
loBody.UpdateString("endTime.dateTime",loWhen.GetAsIso8601("YYYY-MM-DDThh:mm:ss",0))
loBody.UpdateString("endTime.timeZone","UTC")
lnSuccess = loHttp.HttpJson("POST","https://graph.microsoft.com/v1.0/me/calendar/getSchedule",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 loWhen
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 loWhen
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 loWhen
CANCEL
ENDIF
IF (loResult.JsonTypeOf("value") <> 4) THEN
? "Expected schedule results."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loWhen
CANCEL
ENDIF
i = 0
DO WHILE (i < loResult.SizeOfArray("value"))
loResult.I = i
? "Schedule: " + loResult.StringOf("value[i].scheduleId")
IF (loResult.JsonTypeOf("value[i].error") = 3) THEN
? "Schedule error: " + loResult.StringOf("value[i].error.responseCode")
ELSE
* Keep the slot sequence as a string so leading zeroes are retained.
? "Availability: " + loResult.StringOf("value[i].availabilityView")
j = 0
DO WHILE (j < loResult.SizeOfArray("value[i].scheduleItems"))
loResult.J = j
? "Status: " + loResult.StringOf("value[i].scheduleItems[j].status")
? "Start: " + loResult.StringOf("value[i].scheduleItems[j].start.dateTime")
? "End: " + loResult.StringOf("value[i].scheduleItems[j].end.dateTime")
j = j + 1
ENDDO
ENDIF
i = i + 1
ENDDO
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loWhen