Sample code for 30+ languages & platforms
AutoIt Requires Chilkat v11.6.0+

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.

Microsoft documentation

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

;  Assumes Chilkat is unlocked. See Global Unlock Sample.
$bSuccess = False
;  Obtain a delegated Microsoft Graph token using the calendar authorization example.
;  See Authorize Outlook Calendar with OAuth2 and PKCE.
$oToken = ObjCreate("Chilkat.JsonObject")
$oToken.Strict = True
$bSuccess = $oToken.LoadFile("qa_data/tokens/outlookCalendar.json")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not read the private calendar token cache." & @CRLF)
    Exit
EndIf

If (($oToken.JsonTypeOf("access_token") <> 1) Or ($oToken.StringOfEquals("access_token","",True) = True)) Then
    ConsoleWrite("Expected a nonempty access_token in the JSON." & @CRLF)
    Exit
EndIf

$oHttp = ObjCreate("Chilkat.Http")
$oHttp.AuthToken = $oToken.StringOf("access_token")
$oHttp.RequireSslCertVerify = True
$oHttp.FollowRedirects = False
$oHttp.ConnectTimeout = 20
$oHttp.ReadTimeout = 30
$oHttp.SetRequestHeader "Accept","application/json"
$oResp = ObjCreate("Chilkat.HttpResponse")
$oResult = ObjCreate("Chilkat.JsonObject")
$oResult.Strict = True
$oHttp.SetRequestHeader "Prefer","outlook.timezone=""UTC"", IdType=""ImmutableId"""
$oBody = ObjCreate("Chilkat.JsonObject")
$oBody.UpdateString("schedules[0]","user@example.com")
$oBody.UpdateString("schedules[1]","room@example.com")
$oBody.UpdateInt("availabilityViewInterval",30)
$oWhen = ObjCreate("Chilkat.CkDateTime")
$bSuccess = $oWhen.SetFromCurrentSystemTime()
If ($bSuccess <> True) Then
    ConsoleWrite("Could not get the window start." & @CRLF)
    Exit
EndIf

$oBody.UpdateString("startTime.dateTime",$oWhen.GetAsIso8601("YYYY-MM-DDThh:mm:ss",False))
$oBody.UpdateString("startTime.timeZone","UTC")
$bSuccess = $oWhen.AddSeconds(28800)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not set the eight-hour window." & @CRLF)
    Exit
EndIf

$oBody.UpdateString("endTime.dateTime",$oWhen.GetAsIso8601("YYYY-MM-DDThh:mm:ss",False))
$oBody.UpdateString("endTime.timeZone","UTC")
$bSuccess = $oHttp.HttpJson("POST","https://graph.microsoft.com/v1.0/me/calendar/getSchedule",$oBody,"application/json",$oResp)
;  A false result can still include an HTTP error response.
If (($bSuccess <> True) And ($oResp.StatusCode = 0)) Then
    ConsoleWrite("No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying." & @CRLF)
    Exit
EndIf

If ($oResp.StatusCode <> 200) Then
    ConsoleWrite("Unexpected HTTP status: " & $oResp.StatusCode & @CRLF)
    ConsoleWrite("Request ID: " & $oResp.GetHeaderField("request-id") & @CRLF)
    If ($oResp.StatusCode = 429) Then
        ConsoleWrite("Retry-After: " & $oResp.GetHeaderField("Retry-After") & @CRLF)
    EndIf

    If ($oResp.StatusCode = 401) Then
        ConsoleWrite("Refresh the Graph access token or authorize again." & @CRLF)
    EndIf

    Exit
EndIf

$bSuccess = $oResult.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
    ConsoleWrite("Expected a well-formed JSON response." & @CRLF)
    Exit
EndIf

If ($oResult.JsonTypeOf("value") <> 4) Then
    ConsoleWrite("Expected schedule results." & @CRLF)
    Exit
EndIf

Local $i = 0
While ($i < $oResult.SizeOfArray("value"))
    $oResult.I = $i
    ConsoleWrite("Schedule: " & $oResult.StringOf("value[i].scheduleId") & @CRLF)
    If ($oResult.JsonTypeOf("value[i].error") = 3) Then
        ConsoleWrite("Schedule error: " & $oResult.StringOf("value[i].error.responseCode") & @CRLF)
    Else
        ;  Keep the slot sequence as a string so leading zeroes are retained.
        ConsoleWrite("Availability: " & $oResult.StringOf("value[i].availabilityView") & @CRLF)
Local $iJ = 0
        While ($iJ < $oResult.SizeOfArray("value[i].scheduleItems"))
            $oResult.J = $iJ
            ConsoleWrite("Status: " & $oResult.StringOf("value[i].scheduleItems[j].status") & @CRLF)
            ConsoleWrite("Start: " & $oResult.StringOf("value[i].scheduleItems[j].start.dateTime") & @CRLF)
            ConsoleWrite("End: " & $oResult.StringOf("value[i].scheduleItems[j].end.dateTime") & @CRLF)
            $iJ = $iJ + 1
        Wend
    EndIf

    $i = $i + 1
Wend