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 Lua Downloads
local chilkat = require("chilkat")
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local success = false
-- Obtain a delegated Microsoft Graph token using the calendar authorization example.
-- See Authorize Outlook Calendar with OAuth2 and PKCE.
local token = chilkat.JsonObject.new()
token.strict = true
success = token:load_file("qa_data/tokens/outlookCalendar.json")
if not success then
print("Could not read the private calendar token cache.")
return
end
if (token:json_type_of("access_token") ~= 1) or (token:string_of_equals("access_token", "", true)) then
print("Expected a nonempty access_token in the JSON.")
return
end
local http = chilkat.Http.new()
http.auth_token = token:string_of("access_token")
http.require_ssl_cert_verify = true
http.follow_redirects = false
http.connect_timeout = 20
http.read_timeout = 30
http:set_request_header("Accept", "application/json")
local resp = chilkat.HttpResponse.new()
local result = chilkat.JsonObject.new()
result.strict = true
http:set_request_header("Prefer", "outlook.timezone=\"UTC\", IdType=\"ImmutableId\"")
local body = chilkat.JsonObject.new()
body:update_string("schedules[0]", "user@example.com")
body:update_string("schedules[1]", "room@example.com")
body:update_int("availabilityViewInterval", 30)
local when = chilkat.DateTime.new()
success = when:set_from_current_system_time()
if not success then
print("Could not get the window start.")
return
end
body:update_string("startTime.dateTime", when:get_as_iso8601("YYYY-MM-DDThh:mm:ss", false))
body:update_string("startTime.timeZone", "UTC")
success = when:add_seconds(28800)
if not success then
print("Could not set the eight-hour window.")
return
end
body:update_string("endTime.dateTime", when:get_as_iso8601("YYYY-MM-DDThh:mm:ss", false))
body:update_string("endTime.timeZone", "UTC")
success = http:http_json("POST", "https://graph.microsoft.com/v1.0/me/calendar/getSchedule", body, "application/json", resp)
-- A false result can still include an HTTP error response.
if (not success) and (resp.status_code == 0) then
print("No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying.")
return
end
if resp.status_code ~= 200 then
print("Unexpected HTTP status: " .. resp.status_code)
print("Request ID: " .. resp:get_header_field("request-id"))
if resp.status_code == 429 then
print("Retry-After: " .. resp:get_header_field("Retry-After"))
end
if resp.status_code == 401 then
print("Refresh the Graph access token or authorize again.")
end
return
end
success = result:load(resp.body_str)
if not success then
print("Expected a well-formed JSON response.")
return
end
if result:json_type_of("value") ~= 4 then
print("Expected schedule results.")
return
end
local i = 0
while i < result:size_of_array("value") do
result.i = i
print("Schedule: " .. result:string_of("value[i].scheduleId"))
if result:json_type_of("value[i].error") == 3 then
print("Schedule error: " .. result:string_of("value[i].error.responseCode"))
else
-- Keep the slot sequence as a string so leading zeroes are retained.
print("Availability: " .. result:string_of("value[i].availabilityView"))
local j = 0
while j < result:size_of_array("value[i].scheduleItems") do
result.j = j
print("Status: " .. result:string_of("value[i].scheduleItems[j].status"))
print("Start: " .. result:string_of("value[i].scheduleItems[j].start.dateTime"))
print("End: " .. result:string_of("value[i].scheduleItems[j].end.dateTime"))
j = j + 1
end
end
i = i + 1
end