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 Unicode C Downloads
#include <C_CkJsonObjectW.h>
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkFileAccessW.h>
#include <C_CkDateTimeW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkCrypt2W.h>
void ChilkatSample(void)
{
BOOL success;
HCkJsonObjectW token;
HCkHttpW http;
HCkHttpResponseW resp;
HCkJsonObjectW result;
const wchar_t *requestFile;
const wchar_t *resultFile;
HCkFileAccessW fac;
HCkJsonObjectW body;
HCkDateTimeW day;
HCkStringBuilderW midnight;
HCkCrypt2W crypt;
const wchar_t *transactionId;
HCkJsonObjectW receipt;
HCkJsonObjectW saved;
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 = CkJsonObjectW_Create();
CkJsonObjectW_putStrict(token,TRUE);
success = CkJsonObjectW_LoadFile(token,L"qa_data/tokens/outlookCalendar.json");
if (success != TRUE) {
wprintf(L"Could not read the private calendar token cache.\n");
CkJsonObjectW_Dispose(token);
return;
}
if ((CkJsonObjectW_JsonTypeOf(token,L"access_token") != 1) || (CkJsonObjectW_StringOfEquals(token,L"access_token",L"",TRUE) == TRUE)) {
wprintf(L"Expected a nonempty access_token in the JSON.\n");
CkJsonObjectW_Dispose(token);
return;
}
http = CkHttpW_Create();
CkHttpW_putAuthToken(http,CkJsonObjectW_stringOf(token,L"access_token"));
CkHttpW_putRequireSslCertVerify(http,TRUE);
CkHttpW_putFollowRedirects(http,FALSE);
CkHttpW_putConnectTimeout(http,20);
CkHttpW_putReadTimeout(http,30);
CkHttpW_SetRequestHeader(http,L"Accept",L"application/json");
resp = CkHttpResponseW_Create();
result = CkJsonObjectW_Create();
CkJsonObjectW_putStrict(result,TRUE);
CkHttpW_SetRequestHeader(http,L"Prefer",L"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 = L"qa_data/outlookCalendar/create-all-day-request.json";
resultFile = L"qa_data/outlookCalendar/created-all-day.json";
fac = CkFileAccessW_Create();
success = CkFileAccessW_DirEnsureExists(fac,L"qa_data/outlookCalendar");
if (success != TRUE) {
wprintf(L"Could not create the private operation directory.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(result);
CkFileAccessW_Dispose(fac);
return;
}
body = CkJsonObjectW_Create();
CkJsonObjectW_putStrict(body,TRUE);
if (CkFileAccessW_FileExists(fac,requestFile) == TRUE) {
success = CkJsonObjectW_LoadFile(body,requestFile);
if (success != TRUE) {
wprintf(L"Could not read the saved create request; nothing sent.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(result);
CkFileAccessW_Dispose(fac);
CkJsonObjectW_Dispose(body);
return;
}
// Retry the same saved body and transactionId after an uncertain result.
}
else {
day = CkDateTimeW_Create();
success = CkDateTimeW_SetFromCurrentSystemTime(day);
if (success != TRUE) {
wprintf(L"Could not get the date.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(result);
CkFileAccessW_Dispose(fac);
CkJsonObjectW_Dispose(body);
CkDateTimeW_Dispose(day);
return;
}
success = CkDateTimeW_AddDays(day,1);
if (success != TRUE) {
wprintf(L"Could not choose the start date.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(result);
CkFileAccessW_Dispose(fac);
CkJsonObjectW_Dispose(body);
CkDateTimeW_Dispose(day);
return;
}
midnight = CkStringBuilderW_Create();
CkStringBuilderW_Append(midnight,CkDateTimeW_getAsIso8601(day,L"YYYY-MM-DD",FALSE));
CkStringBuilderW_Append(midnight,L"T00:00:00");
CkJsonObjectW_UpdateString(body,L"start.dateTime",CkStringBuilderW_getAsString(midnight));
CkJsonObjectW_UpdateString(body,L"start.timeZone",L"UTC");
success = CkDateTimeW_AddDays(day,2);
if (success != TRUE) {
wprintf(L"Could not calculate the exclusive end date.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(result);
CkFileAccessW_Dispose(fac);
CkJsonObjectW_Dispose(body);
CkDateTimeW_Dispose(day);
CkStringBuilderW_Dispose(midnight);
return;
}
CkStringBuilderW_Clear(midnight);
CkStringBuilderW_Append(midnight,CkDateTimeW_getAsIso8601(day,L"YYYY-MM-DD",FALSE));
CkStringBuilderW_Append(midnight,L"T00:00:00");
CkJsonObjectW_UpdateString(body,L"end.dateTime",CkStringBuilderW_getAsString(midnight));
CkJsonObjectW_UpdateString(body,L"end.timeZone",L"UTC");
CkJsonObjectW_UpdateString(body,L"subject",L"All-day event");
CkJsonObjectW_UpdateBool(body,L"isAllDay",TRUE);
CkJsonObjectW_UpdateString(body,L"showAs",L"free");
// No attendees: this is an appointment, so the request does not send invitations.
crypt = CkCrypt2W_Create();
transactionId = CkCrypt2W_generateUuid(crypt);
if (CkCrypt2W_getLastMethodSuccess(crypt) != TRUE) {
wprintf(L"Could not create a transaction ID.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(result);
CkFileAccessW_Dispose(fac);
CkJsonObjectW_Dispose(body);
CkDateTimeW_Dispose(day);
CkStringBuilderW_Dispose(midnight);
CkCrypt2W_Dispose(crypt);
return;
}
CkJsonObjectW_UpdateString(body,L"transactionId",transactionId);
success = CkJsonObjectW_WriteFile(body,requestFile);
if (success != TRUE) {
wprintf(L"Could not save the request before sending; nothing sent.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(result);
CkFileAccessW_Dispose(fac);
CkJsonObjectW_Dispose(body);
CkDateTimeW_Dispose(day);
CkStringBuilderW_Dispose(midnight);
CkCrypt2W_Dispose(crypt);
return;
}
}
if ((CkJsonObjectW_JsonTypeOf(body,L"transactionId") != 1) || (CkJsonObjectW_StringOfEquals(body,L"transactionId",L"",TRUE) == TRUE)) {
wprintf(L"Expected a nonempty transactionId in the JSON.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(result);
CkFileAccessW_Dispose(fac);
CkJsonObjectW_Dispose(body);
CkDateTimeW_Dispose(day);
CkStringBuilderW_Dispose(midnight);
CkCrypt2W_Dispose(crypt);
return;
}
// A previous successful run leaves a small receipt. Do not submit it again.
if (CkFileAccessW_FileExists(fac,resultFile) == TRUE) {
receipt = CkJsonObjectW_Create();
CkJsonObjectW_putStrict(receipt,TRUE);
success = CkJsonObjectW_LoadFile(receipt,resultFile);
if (success != TRUE) {
wprintf(L"Could not read the existing receipt; nothing sent.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(result);
CkFileAccessW_Dispose(fac);
CkJsonObjectW_Dispose(body);
CkDateTimeW_Dispose(day);
CkStringBuilderW_Dispose(midnight);
CkCrypt2W_Dispose(crypt);
CkJsonObjectW_Dispose(receipt);
return;
}
if ((CkJsonObjectW_JsonTypeOf(receipt,L"id") != 1) || (CkJsonObjectW_StringOfEquals(receipt,L"id",L"",TRUE) == TRUE)) {
wprintf(L"Expected a nonempty id in the JSON.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(result);
CkFileAccessW_Dispose(fac);
CkJsonObjectW_Dispose(body);
CkDateTimeW_Dispose(day);
CkStringBuilderW_Dispose(midnight);
CkCrypt2W_Dispose(crypt);
CkJsonObjectW_Dispose(receipt);
return;
}
if (CkJsonObjectW_StringOfEquals(receipt,L"transactionId",CkJsonObjectW_stringOf(body,L"transactionId"),TRUE) != TRUE) {
wprintf(L"The receipt belongs to another operation; nothing sent.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(result);
CkFileAccessW_Dispose(fac);
CkJsonObjectW_Dispose(body);
CkDateTimeW_Dispose(day);
CkStringBuilderW_Dispose(midnight);
CkCrypt2W_Dispose(crypt);
CkJsonObjectW_Dispose(receipt);
return;
}
wprintf(L"Already created event ID: %s\n",CkJsonObjectW_stringOf(receipt,L"id"));
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(result);
CkFileAccessW_Dispose(fac);
CkJsonObjectW_Dispose(body);
CkDateTimeW_Dispose(day);
CkStringBuilderW_Dispose(midnight);
CkCrypt2W_Dispose(crypt);
CkJsonObjectW_Dispose(receipt);
return;
}
success = CkHttpW_HttpJson(http,L"POST",L"https://graph.microsoft.com/v1.0/me/events",body,L"application/json",resp);
// A false result can still include an HTTP error response.
if ((success != TRUE) && (CkHttpResponseW_getStatusCode(resp) == 0)) {
wprintf(L"No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(result);
CkFileAccessW_Dispose(fac);
CkJsonObjectW_Dispose(body);
CkDateTimeW_Dispose(day);
CkStringBuilderW_Dispose(midnight);
CkCrypt2W_Dispose(crypt);
CkJsonObjectW_Dispose(receipt);
return;
}
if (CkHttpResponseW_getStatusCode(resp) != 201) {
wprintf(L"Unexpected HTTP status: %d\n",CkHttpResponseW_getStatusCode(resp));
wprintf(L"Request ID: %s\n",CkHttpResponseW_getHeaderField(resp,L"request-id"));
if (CkHttpResponseW_getStatusCode(resp) == 429) {
wprintf(L"Retry-After: %s\n",CkHttpResponseW_getHeaderField(resp,L"Retry-After"));
}
if (CkHttpResponseW_getStatusCode(resp) == 401) {
wprintf(L"Refresh the Graph access token or authorize again.\n");
}
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(result);
CkFileAccessW_Dispose(fac);
CkJsonObjectW_Dispose(body);
CkDateTimeW_Dispose(day);
CkStringBuilderW_Dispose(midnight);
CkCrypt2W_Dispose(crypt);
CkJsonObjectW_Dispose(receipt);
return;
}
success = CkJsonObjectW_Load(result,CkHttpResponseW_bodyStr(resp));
if (success != TRUE) {
wprintf(L"Expected a well-formed JSON response.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(result);
CkFileAccessW_Dispose(fac);
CkJsonObjectW_Dispose(body);
CkDateTimeW_Dispose(day);
CkStringBuilderW_Dispose(midnight);
CkCrypt2W_Dispose(crypt);
CkJsonObjectW_Dispose(receipt);
return;
}
if ((CkJsonObjectW_JsonTypeOf(result,L"id") != 1) || (CkJsonObjectW_StringOfEquals(result,L"id",L"",TRUE) == TRUE)) {
wprintf(L"Expected a nonempty id in the JSON.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(result);
CkFileAccessW_Dispose(fac);
CkJsonObjectW_Dispose(body);
CkDateTimeW_Dispose(day);
CkStringBuilderW_Dispose(midnight);
CkCrypt2W_Dispose(crypt);
CkJsonObjectW_Dispose(receipt);
return;
}
saved = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(saved,L"id",CkJsonObjectW_stringOf(result,L"id"));
CkJsonObjectW_UpdateString(saved,L"transactionId",CkJsonObjectW_stringOf(body,L"transactionId"));
// Display the ID even if saving the receipt fails: the server has already created the event.
wprintf(L"Created event ID: %s\n",CkJsonObjectW_stringOf(result,L"id"));
success = CkJsonObjectW_WriteFile(saved,resultFile);
if (success != TRUE) {
wprintf(L"The event was created, but its receipt could not be saved. Reconcile before retrying.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(result);
CkFileAccessW_Dispose(fac);
CkJsonObjectW_Dispose(body);
CkDateTimeW_Dispose(day);
CkStringBuilderW_Dispose(midnight);
CkCrypt2W_Dispose(crypt);
CkJsonObjectW_Dispose(receipt);
CkJsonObjectW_Dispose(saved);
return;
}
wprintf(L"Start: %s (%s)\n",CkJsonObjectW_stringOf(result,L"start.dateTime"),CkJsonObjectW_stringOf(result,L"start.timeZone"));
wprintf(L"End: %s (%s)\n",CkJsonObjectW_stringOf(result,L"end.dateTime"),CkJsonObjectW_stringOf(result,L"end.timeZone"));
wprintf(L"Event receipt saved.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(result);
CkFileAccessW_Dispose(fac);
CkJsonObjectW_Dispose(body);
CkDateTimeW_Dispose(day);
CkStringBuilderW_Dispose(midnight);
CkCrypt2W_Dispose(crypt);
CkJsonObjectW_Dispose(receipt);
CkJsonObjectW_Dispose(saved);
}