Outlook Calendar: Create an All-Day or Multi-Day Event
See more Outlook Calendar Examples
Create a two-day all-day event starting tomorrow in UTC. Both boundaries are midnight in the same time zone, and the end is exclusive: a one-day event ends at midnight on the next calendar date. Change the two-day span to one for a single-day event. showAs="free" is an explicit example choice; use busy or oof when appropriate.
All-day dates are calendar dates, not fixed-duration local hours. When adapting to a local zone, use local midnight boundaries and calendar-date arithmetic; do not assume every daylight-saving day contains 24 hours. The example uses UTC so adding days does not cross a DST offset change.
Live creation: this posts a real calendar item. The request file is saved before sending, including its transactionId, and a receipt records the returned ID. Reuse the saved payload after an uncertain outcome; use a new filename pair for a deliberately new operation. A saved receipt prevents repeat creation on a later successful run. Keep request/receipt files private, per account, and use one writer. See the basic creation example for the same pattern.
Delegated permission: Calendars.ReadWrite.
Uses Microsoft Graph v1.0 in the global cloud with a delegated access token saved by the calendar OAuth2 example. Both personal Outlook.com accounts and Microsoft 365 work/school accounts with a mailbox can use this flow. The app registration must support the selected account type. A token for SMTP/IMAP/POP or another API is not a Graph token.
/me means the signed-in user and requires delegated authentication. An unattended Microsoft 365 service instead needs application permissions and administrator consent, and addresses an explicit mailbox through /users/{id-or-userPrincipalName}. That client-credentials flow does not apply to personal Outlook.com accounts. These samples target the global cloud; national clouds use different authority and Graph hosts.
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 Delphi DLL Downloads
var
success: Boolean;
token: HCkJsonObject;
http: HCkHttp;
resp: HCkHttpResponse;
result: HCkJsonObject;
requestFile: PWideChar;
resultFile: PWideChar;
fac: HCkFileAccess;
body: HCkJsonObject;
day: HCkDateTime;
midnight: HCkStringBuilder;
crypt: HCkCrypt2;
transactionId: PWideChar;
receipt: HCkJsonObject;
saved: HCkJsonObject;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success := False;
// Obtain a delegated Microsoft Graph token using the calendar authorization example.
// See Authorize Outlook Calendar with OAuth2 and PKCE.
token := CkJsonObject_Create();
CkJsonObject_putStrict(token,True);
success := CkJsonObject_LoadFile(token,'qa_data/tokens/outlookCalendar.json');
if (success <> True) then
begin
Memo1.Lines.Add('Could not read the private calendar token cache.');
Exit;
end;
if ((CkJsonObject_JsonTypeOf(token,'access_token') <> 1) or (CkJsonObject_StringOfEquals(token,'access_token','',True) = True)) then
begin
Memo1.Lines.Add('Expected a nonempty access_token in the JSON.');
Exit;
end;
http := CkHttp_Create();
CkHttp_putAuthToken(http,CkJsonObject__stringOf(token,'access_token'));
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putFollowRedirects(http,False);
CkHttp_putConnectTimeout(http,20);
CkHttp_putReadTimeout(http,30);
CkHttp_SetRequestHeader(http,'Accept','application/json');
resp := CkHttpResponse_Create();
result := CkJsonObject_Create();
CkJsonObject_putStrict(result,True);
CkHttp_SetRequestHeader(http,'Prefer','outlook.timezone="UTC", IdType="ImmutableId"');
// Live write: creates an appointment in the signed-in user's default calendar.
// Keep one request file per logical operation and account. Do not run concurrent copies.
requestFile := 'qa_data/outlookCalendar/create-all-day-request.json';
resultFile := 'qa_data/outlookCalendar/created-all-day.json';
fac := CkFileAccess_Create();
success := CkFileAccess_DirEnsureExists(fac,'qa_data/outlookCalendar');
if (success <> True) then
begin
Memo1.Lines.Add('Could not create the private operation directory.');
Exit;
end;
body := CkJsonObject_Create();
CkJsonObject_putStrict(body,True);
if (CkFileAccess_FileExists(fac,requestFile) = True) then
begin
success := CkJsonObject_LoadFile(body,requestFile);
if (success <> True) then
begin
Memo1.Lines.Add('Could not read the saved create request; nothing sent.');
Exit;
end;
// Retry the same saved body and transactionId after an uncertain result.
end
else
begin
day := CkDateTime_Create();
success := CkDateTime_SetFromCurrentSystemTime(day);
if (success <> True) then
begin
Memo1.Lines.Add('Could not get the date.');
Exit;
end;
success := CkDateTime_AddDays(day,1);
if (success <> True) then
begin
Memo1.Lines.Add('Could not choose the start date.');
Exit;
end;
midnight := CkStringBuilder_Create();
CkStringBuilder_Append(midnight,CkDateTime__getAsIso8601(day,'YYYY-MM-DD',False));
CkStringBuilder_Append(midnight,'T00:00:00');
CkJsonObject_UpdateString(body,'start.dateTime',CkStringBuilder__getAsString(midnight));
CkJsonObject_UpdateString(body,'start.timeZone','UTC');
success := CkDateTime_AddDays(day,2);
if (success <> True) then
begin
Memo1.Lines.Add('Could not calculate the exclusive end date.');
Exit;
end;
CkStringBuilder_Clear(midnight);
CkStringBuilder_Append(midnight,CkDateTime__getAsIso8601(day,'YYYY-MM-DD',False));
CkStringBuilder_Append(midnight,'T00:00:00');
CkJsonObject_UpdateString(body,'end.dateTime',CkStringBuilder__getAsString(midnight));
CkJsonObject_UpdateString(body,'end.timeZone','UTC');
CkJsonObject_UpdateString(body,'subject','All-day event');
CkJsonObject_UpdateBool(body,'isAllDay',True);
CkJsonObject_UpdateString(body,'showAs','free');
// No attendees: this is an appointment, so the request does not send invitations.
crypt := CkCrypt2_Create();
transactionId := CkCrypt2__generateUuid(crypt);
if (CkCrypt2_getLastMethodSuccess(crypt) <> True) then
begin
Memo1.Lines.Add('Could not create a transaction ID.');
Exit;
end;
CkJsonObject_UpdateString(body,'transactionId',transactionId);
success := CkJsonObject_WriteFile(body,requestFile);
if (success <> True) then
begin
Memo1.Lines.Add('Could not save the request before sending; nothing sent.');
Exit;
end;
end;
if ((CkJsonObject_JsonTypeOf(body,'transactionId') <> 1) or (CkJsonObject_StringOfEquals(body,'transactionId','',True) = True)) then
begin
Memo1.Lines.Add('Expected a nonempty transactionId in the JSON.');
Exit;
end;
// A previous successful run leaves a small receipt. Do not submit it again.
if (CkFileAccess_FileExists(fac,resultFile) = True) then
begin
receipt := CkJsonObject_Create();
CkJsonObject_putStrict(receipt,True);
success := CkJsonObject_LoadFile(receipt,resultFile);
if (success <> True) then
begin
Memo1.Lines.Add('Could not read the existing receipt; nothing sent.');
Exit;
end;
if ((CkJsonObject_JsonTypeOf(receipt,'id') <> 1) or (CkJsonObject_StringOfEquals(receipt,'id','',True) = True)) then
begin
Memo1.Lines.Add('Expected a nonempty id in the JSON.');
Exit;
end;
if (CkJsonObject_StringOfEquals(receipt,'transactionId',CkJsonObject__stringOf(body,'transactionId'),True) <> True) then
begin
Memo1.Lines.Add('The receipt belongs to another operation; nothing sent.');
Exit;
end;
Memo1.Lines.Add('Already created event ID: ' + CkJsonObject__stringOf(receipt,'id'));
Exit;
end;
success := CkHttp_HttpJson(http,'POST','https://graph.microsoft.com/v1.0/me/events',body,'application/json',resp);
// A false result can still include an HTTP error response.
if ((success <> True) and (CkHttpResponse_getStatusCode(resp) = 0)) then
begin
Memo1.Lines.Add('No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying.');
Exit;
end;
if (CkHttpResponse_getStatusCode(resp) <> 201) then
begin
Memo1.Lines.Add('Unexpected HTTP status: ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
Memo1.Lines.Add('Request ID: ' + CkHttpResponse__getHeaderField(resp,'request-id'));
if (CkHttpResponse_getStatusCode(resp) = 429) then
begin
Memo1.Lines.Add('Retry-After: ' + CkHttpResponse__getHeaderField(resp,'Retry-After'));
end;
if (CkHttpResponse_getStatusCode(resp) = 401) then
begin
Memo1.Lines.Add('Refresh the Graph access token or authorize again.');
end;
Exit;
end;
success := CkJsonObject_Load(result,CkHttpResponse__bodyStr(resp));
if (success <> True) then
begin
Memo1.Lines.Add('Expected a well-formed JSON response.');
Exit;
end;
if ((CkJsonObject_JsonTypeOf(result,'id') <> 1) or (CkJsonObject_StringOfEquals(result,'id','',True) = True)) then
begin
Memo1.Lines.Add('Expected a nonempty id in the JSON.');
Exit;
end;
saved := CkJsonObject_Create();
CkJsonObject_UpdateString(saved,'id',CkJsonObject__stringOf(result,'id'));
CkJsonObject_UpdateString(saved,'transactionId',CkJsonObject__stringOf(body,'transactionId'));
// Display the ID even if saving the receipt fails: the server has already created the event.
Memo1.Lines.Add('Created event ID: ' + CkJsonObject__stringOf(result,'id'));
success := CkJsonObject_WriteFile(saved,resultFile);
if (success <> True) then
begin
Memo1.Lines.Add('The event was created, but its receipt could not be saved. Reconcile before retrying.');
Exit;
end;
Memo1.Lines.Add('Start: ' + CkJsonObject__stringOf(result,'start.dateTime') + ' (' + CkJsonObject__stringOf(result,'start.timeZone') + ')');
Memo1.Lines.Add('End: ' + CkJsonObject__stringOf(result,'end.dateTime') + ' (' + CkJsonObject__stringOf(result,'end.timeZone') + ')');
Memo1.Lines.Add('Event receipt saved.');
CkJsonObject_Dispose(token);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkFileAccess_Dispose(fac);
CkJsonObject_Dispose(body);
CkDateTime_Dispose(day);
CkStringBuilder_Dispose(midnight);
CkCrypt2_Dispose(crypt);
CkJsonObject_Dispose(receipt);
CkJsonObject_Dispose(saved);