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

Delphi ActiveX
var
success: Integer;
token: TChilkatJsonObject;
http: TChilkatHttp;
resp: TChilkatHttpResponse;
result: TChilkatJsonObject;
body: TChilkatJsonObject;
when: TCkDateTime;
i: Integer;
j: Integer;

begin
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.
token := TChilkatJsonObject.Create(Self);
token.Strict := 1;
success := token.LoadFile('qa_data/tokens/outlookCalendar.json');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not read the private calendar token cache.');
    Exit;
  end;
if ((token.JsonTypeOf('access_token') <> 1) or (token.StringOfEquals('access_token','',1) = 1)) then
  begin
    Memo1.Lines.Add('Expected a nonempty access_token in the JSON.');
    Exit;
  end;
http := TChilkatHttp.Create(Self);
http.AuthToken := token.StringOf('access_token');
http.RequireSslCertVerify := 1;
http.FollowRedirects := 0;
http.ConnectTimeout := 20;
http.ReadTimeout := 30;
http.SetRequestHeader('Accept','application/json');
resp := TChilkatHttpResponse.Create(Self);
result := TChilkatJsonObject.Create(Self);
result.Strict := 1;
http.SetRequestHeader('Prefer','outlook.timezone="UTC", IdType="ImmutableId"');
body := TChilkatJsonObject.Create(Self);
body.UpdateString('schedules[0]','user@example.com');
body.UpdateString('schedules[1]','room@example.com');
body.UpdateInt('availabilityViewInterval',30);
when := TCkDateTime.Create(Self);
success := when.SetFromCurrentSystemTime();
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not get the window start.');
    Exit;
  end;
body.UpdateString('startTime.dateTime',when.GetAsIso8601('YYYY-MM-DDThh:mm:ss',0));
body.UpdateString('startTime.timeZone','UTC');
success := when.AddSeconds(28800);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not set the eight-hour window.');
    Exit;
  end;
body.UpdateString('endTime.dateTime',when.GetAsIso8601('YYYY-MM-DDThh:mm:ss',0));
body.UpdateString('endTime.timeZone','UTC');
success := http.HttpJson('POST','https://graph.microsoft.com/v1.0/me/calendar/getSchedule',body.ControlInterface,'application/json',resp.ControlInterface);
//  A false result can still include an HTTP error response.
if ((success <> 1) and (resp.StatusCode = 0)) then
  begin
    Memo1.Lines.Add('No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying.');
    Exit;
  end;
if (resp.StatusCode <> 200) then
  begin
    Memo1.Lines.Add('Unexpected HTTP status: ' + IntToStr(resp.StatusCode));
    Memo1.Lines.Add('Request ID: ' + resp.GetHeaderField('request-id'));
    if (resp.StatusCode = 429) then
      begin
        Memo1.Lines.Add('Retry-After: ' + resp.GetHeaderField('Retry-After'));
      end;
    if (resp.StatusCode = 401) then
      begin
        Memo1.Lines.Add('Refresh the Graph access token or authorize again.');
      end;
    Exit;
  end;
success := result.Load(resp.BodyStr);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Expected a well-formed JSON response.');
    Exit;
  end;
if (result.JsonTypeOf('value') <> 4) then
  begin
    Memo1.Lines.Add('Expected schedule results.');
    Exit;
  end;
i := 0;
while (i < result.SizeOfArray('value')) do
  begin
    result.I := i;
    Memo1.Lines.Add('Schedule: ' + result.StringOf('value[i].scheduleId'));
    if (result.JsonTypeOf('value[i].error') = 3) then
      begin
        Memo1.Lines.Add('Schedule error: ' + result.StringOf('value[i].error.responseCode'));
      end
    else
      begin
        //  Keep the slot sequence as a string so leading zeroes are retained.
        Memo1.Lines.Add('Availability: ' + result.StringOf('value[i].availabilityView'));
        j := 0;
        while (j < result.SizeOfArray('value[i].scheduleItems')) do
          begin
            result.J := j;
            Memo1.Lines.Add('Status: ' + result.StringOf('value[i].scheduleItems[j].status'));
            Memo1.Lines.Add('Start: ' + result.StringOf('value[i].scheduleItems[j].start.dateTime'));
            Memo1.Lines.Add('End: ' + result.StringOf('value[i].scheduleItems[j].end.dateTime'));
            j := j + 1;
          end;

      end;
    i := i + 1;
  end;