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 Classic ASP Downloads
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0
' Assumes Chilkat is unlocked. See Global Unlock Sample.
success = 0
' Obtain a delegated Microsoft Graph token using the calendar authorization example.
' See Authorize Outlook Calendar with OAuth2 and PKCE.
set token = Server.CreateObject("Chilkat.JsonObject")
token.Strict = 1
success = token.LoadFile("qa_data/tokens/outlookCalendar.json")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not read the private calendar token cache.") & "</pre>"
Response.End
End If
If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token","",1) = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Expected a nonempty access_token in the JSON.") & "</pre>"
Response.End
End If
set http = Server.CreateObject("Chilkat.Http")
http.AuthToken = token.StringOf("access_token")
http.RequireSslCertVerify = 1
http.FollowRedirects = 0
http.ConnectTimeout = 20
http.ReadTimeout = 30
http.SetRequestHeader "Accept","application/json"
set resp = Server.CreateObject("Chilkat.HttpResponse")
set result = Server.CreateObject("Chilkat.JsonObject")
result.Strict = 1
http.SetRequestHeader "Prefer","outlook.timezone=""UTC"", IdType=""ImmutableId"""
set body = Server.CreateObject("Chilkat.JsonObject")
success = body.UpdateString("schedules[0]","user@example.com")
success = body.UpdateString("schedules[1]","room@example.com")
success = body.UpdateInt("availabilityViewInterval",30)
set when = Server.CreateObject("Chilkat.CkDateTime")
success = when.SetFromCurrentSystemTime()
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not get the window start.") & "</pre>"
Response.End
End If
success = body.UpdateString("startTime.dateTime",when.GetAsIso8601("YYYY-MM-DDThh:mm:ss",0))
success = body.UpdateString("startTime.timeZone","UTC")
success = when.AddSeconds(28800)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set the eight-hour window.") & "</pre>"
Response.End
End If
success = body.UpdateString("endTime.dateTime",when.GetAsIso8601("YYYY-MM-DDThh:mm:ss",0))
success = body.UpdateString("endTime.timeZone","UTC")
success = http.HttpJson("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 ((success <> 1) And (resp.StatusCode = 0)) Then
Response.Write "<pre>" & Server.HTMLEncode( "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying.") & "</pre>"
Response.End
End If
If (resp.StatusCode <> 200) Then
Response.Write "<pre>" & Server.HTMLEncode( "Unexpected HTTP status: " & resp.StatusCode) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "Request ID: " & resp.GetHeaderField("request-id")) & "</pre>"
If (resp.StatusCode = 429) Then
Response.Write "<pre>" & Server.HTMLEncode( "Retry-After: " & resp.GetHeaderField("Retry-After")) & "</pre>"
End If
If (resp.StatusCode = 401) Then
Response.Write "<pre>" & Server.HTMLEncode( "Refresh the Graph access token or authorize again.") & "</pre>"
End If
Response.End
End If
success = result.Load(resp.BodyStr)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Expected a well-formed JSON response.") & "</pre>"
Response.End
End If
If (result.JsonTypeOf("value") <> 4) Then
Response.Write "<pre>" & Server.HTMLEncode( "Expected schedule results.") & "</pre>"
Response.End
End If
i = 0
Do While (i < result.SizeOfArray("value"))
result.I = i
Response.Write "<pre>" & Server.HTMLEncode( "Schedule: " & result.StringOf("value[i].scheduleId")) & "</pre>"
If (result.JsonTypeOf("value[i].error") = 3) Then
Response.Write "<pre>" & Server.HTMLEncode( "Schedule error: " & result.StringOf("value[i].error.responseCode")) & "</pre>"
Else
' Keep the slot sequence as a string so leading zeroes are retained.
Response.Write "<pre>" & Server.HTMLEncode( "Availability: " & result.StringOf("value[i].availabilityView")) & "</pre>"
j = 0
Do While (j < result.SizeOfArray("value[i].scheduleItems"))
result.J = j
Response.Write "<pre>" & Server.HTMLEncode( "Status: " & result.StringOf("value[i].scheduleItems[j].status")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "Start: " & result.StringOf("value[i].scheduleItems[j].start.dateTime")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "End: " & result.StringOf("value[i].scheduleItems[j].end.dateTime")) & "</pre>"
j = j + 1
Loop
End If
i = i + 1
Loop
%>
</body>
</html>