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 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_When
long i
long j
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
li_Success = 0
// Obtain a delegated Microsoft Graph token using the calendar authorization example.
// See Authorize Outlook Calendar 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/outlookCalendar.json")
if li_Success <> 1 then
Write-Debug "Could not read the private calendar 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","outlook.timezone=~"UTC~", IdType=~"ImmutableId~"")
loo_Body = create oleobject
li_rc = loo_Body.ConnectToNewObject("Chilkat.JsonObject")
loo_Body.UpdateString("schedules[0]","user@example.com")
loo_Body.UpdateString("schedules[1]","room@example.com")
loo_Body.UpdateInt("availabilityViewInterval",30)
loo_When = create oleobject
li_rc = loo_When.ConnectToNewObject("Chilkat.CkDateTime")
li_Success = loo_When.SetFromCurrentSystemTime()
if li_Success <> 1 then
Write-Debug "Could not get the window start."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_When
return
end if
loo_Body.UpdateString("startTime.dateTime",loo_When.GetAsIso8601("YYYY-MM-DDThh:mm:ss",0))
loo_Body.UpdateString("startTime.timeZone","UTC")
li_Success = loo_When.AddSeconds(28800)
if li_Success <> 1 then
Write-Debug "Could not set the eight-hour window."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_When
return
end if
loo_Body.UpdateString("endTime.dateTime",loo_When.GetAsIso8601("YYYY-MM-DDThh:mm:ss",0))
loo_Body.UpdateString("endTime.timeZone","UTC")
li_Success = loo_Http.HttpJson("POST","https://graph.microsoft.com/v1.0/me/calendar/getSchedule",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_When
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_When
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_When
return
end if
if loo_Result.JsonTypeOf("value") <> 4 then
Write-Debug "Expected schedule results."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_When
return
end if
i = 0
do while (i < loo_Result.SizeOfArray("value"))
loo_Result.I = i
Write-Debug "Schedule: " + loo_Result.StringOf("value[i].scheduleId")
if loo_Result.JsonTypeOf("value[i].error") = 3 then
Write-Debug "Schedule error: " + loo_Result.StringOf("value[i].error.responseCode")
else
// Keep the slot sequence as a string so leading zeroes are retained.
Write-Debug "Availability: " + loo_Result.StringOf("value[i].availabilityView")
j = 0
do while (j < loo_Result.SizeOfArray("value[i].scheduleItems"))
loo_Result.J = j
Write-Debug "Status: " + loo_Result.StringOf("value[i].scheduleItems[j].status")
Write-Debug "Start: " + loo_Result.StringOf("value[i].scheduleItems[j].start.dateTime")
Write-Debug "End: " + loo_Result.StringOf("value[i].scheduleItems[j].end.dateTime")
j = j + 1
loop
end if
i = i + 1
loop
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Result
destroy loo_Body
destroy loo_When