Sample code for 30+ languages & platforms
DataFlex 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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoToken
    Handle hoHttp
    Variant vResp
    Handle hoResp
    Handle hoResult
    Variant vBody
    Handle hoBody
    Handle hoWhen
    Integer i
    Integer j
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    Move False To iSuccess
    //  Obtain a delegated Microsoft Graph token using the calendar authorization example.
    //  See Authorize Outlook Calendar with OAuth2 and PKCE.
    Get Create (RefClass(cComChilkatJsonObject)) To hoToken
    If (Not(IsComObjectCreated(hoToken))) Begin
        Send CreateComObject of hoToken
    End
    Set ComStrict Of hoToken To True
    Get ComLoadFile Of hoToken "qa_data/tokens/outlookCalendar.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not read the private calendar token cache."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoToken "access_token" To iTemp1
    Get ComStringOfEquals Of hoToken "access_token" "" True To bTemp1
    If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
        Showln "Expected a nonempty access_token in the JSON."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Get ComStringOf Of hoToken "access_token" To sTemp1
    Set ComAuthToken Of hoHttp To sTemp1
    Set ComRequireSslCertVerify Of hoHttp To True
    Set ComFollowRedirects Of hoHttp To False
    Set ComConnectTimeout Of hoHttp To 20
    Set ComReadTimeout Of hoHttp To 30
    Send ComSetRequestHeader To hoHttp "Accept" "application/json"
    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoResult
    If (Not(IsComObjectCreated(hoResult))) Begin
        Send CreateComObject of hoResult
    End
    Set ComStrict Of hoResult To True
    Send ComSetRequestHeader To hoHttp "Prefer" 'outlook.timezone="UTC", IdType="ImmutableId"'
    Get Create (RefClass(cComChilkatJsonObject)) To hoBody
    If (Not(IsComObjectCreated(hoBody))) Begin
        Send CreateComObject of hoBody
    End
    Get ComUpdateString Of hoBody "schedules[0]" "user@example.com" To iSuccess
    Get ComUpdateString Of hoBody "schedules[1]" "room@example.com" To iSuccess
    Get ComUpdateInt Of hoBody "availabilityViewInterval" 30 To iSuccess
    Get Create (RefClass(cComCkDateTime)) To hoWhen
    If (Not(IsComObjectCreated(hoWhen))) Begin
        Send CreateComObject of hoWhen
    End
    Get ComSetFromCurrentSystemTime Of hoWhen To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not get the window start."
        Procedure_Return
    End

    Get ComGetAsIso8601 Of hoWhen "YYYY-MM-DDThh:mm:ss" False To sTemp1
    Get ComUpdateString Of hoBody "startTime.dateTime" sTemp1 To iSuccess
    Get ComUpdateString Of hoBody "startTime.timeZone" "UTC" To iSuccess
    Get ComAddSeconds Of hoWhen 28800 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set the eight-hour window."
        Procedure_Return
    End

    Get ComGetAsIso8601 Of hoWhen "YYYY-MM-DDThh:mm:ss" False To sTemp1
    Get ComUpdateString Of hoBody "endTime.dateTime" sTemp1 To iSuccess
    Get ComUpdateString Of hoBody "endTime.timeZone" "UTC" To iSuccess
    Get pvComObject of hoBody to vBody
    Get pvComObject of hoResp to vResp
    Get ComHttpJson Of hoHttp "POST" "https://graph.microsoft.com/v1.0/me/calendar/getSchedule" vBody "application/json" vResp To iSuccess
    //  A false result can still include an HTTP error response.
    Get ComStatusCode Of hoResp To iTemp1
    If ((iSuccess <> True) And (iTemp1 = 0)) Begin
        Showln "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying."
        Procedure_Return
    End

    Get ComStatusCode Of hoResp To iTemp1
    If (iTemp1 <> 200) Begin
        Get ComStatusCode Of hoResp To iTemp1
        Showln "Unexpected HTTP status: " iTemp1
        Get ComGetHeaderField Of hoResp "request-id" To sTemp1
        Showln "Request ID: " sTemp1
        Get ComStatusCode Of hoResp To iTemp1
        If (iTemp1 = 429) Begin
            Get ComGetHeaderField Of hoResp "Retry-After" To sTemp1
            Showln "Retry-After: " sTemp1
        End

        Get ComStatusCode Of hoResp To iTemp1
        If (iTemp1 = 401) Begin
            Showln "Refresh the Graph access token or authorize again."
        End

        Procedure_Return
    End

    Get ComBodyStr Of hoResp To sTemp1
    Get ComLoad Of hoResult sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Expected a well-formed JSON response."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoResult "value" To iTemp1
    If (iTemp1 <> 4) Begin
        Showln "Expected schedule results."
        Procedure_Return
    End

    Move 0 To i
    While (i < (ComSizeOfArray(hoResult, "value")))
        Set ComI Of hoResult To i
        Get ComStringOf Of hoResult "value[i].scheduleId" To sTemp1
        Showln "Schedule: " sTemp1
        Get ComJsonTypeOf Of hoResult "value[i].error" To iTemp1
        If (iTemp1 = 3) Begin
            Get ComStringOf Of hoResult "value[i].error.responseCode" To sTemp1
            Showln "Schedule error: " sTemp1
        End
        Else Begin
            //  Keep the slot sequence as a string so leading zeroes are retained.
            Get ComStringOf Of hoResult "value[i].availabilityView" To sTemp1
            Showln "Availability: " sTemp1
            Move 0 To j
            While (j < (ComSizeOfArray(hoResult, "value[i].scheduleItems")))
                Set ComJ Of hoResult To j
                Get ComStringOf Of hoResult "value[i].scheduleItems[j].status" To sTemp1
                Showln "Status: " sTemp1
                Get ComStringOf Of hoResult "value[i].scheduleItems[j].start.dateTime" To sTemp1
                Showln "Start: " sTemp1
                Get ComStringOf Of hoResult "value[i].scheduleItems[j].end.dateTime" To sTemp1
                Showln "End: " sTemp1
                Move (j + 1) To j
            Loop

        End

        Move (i + 1) To i
    Loop



End_Procedure