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 C Downloads
#include <C_CkJsonObject.h>
#include <C_CkHttp.h>
#include <C_CkHttpResponse.h>
#include <C_CkFileAccess.h>
#include <C_CkDateTime.h>
#include <C_CkStringBuilder.h>
#include <C_CkCrypt2.h>
void ChilkatSample(void)
{
BOOL success;
HCkJsonObject token;
HCkHttp http;
HCkHttpResponse resp;
HCkJsonObject result;
const char *requestFile;
const char *resultFile;
HCkFileAccess fac;
HCkJsonObject body;
HCkDateTime day;
HCkStringBuilder midnight;
HCkCrypt2 crypt;
const char *transactionId;
HCkJsonObject receipt;
HCkJsonObject 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 = CkJsonObject_Create();
CkJsonObject_putStrict(token,TRUE);
success = CkJsonObject_LoadFile(token,"qa_data/tokens/outlookCalendar.json");
if (success != TRUE) {
printf("Could not read the private calendar token cache.\n");
CkJsonObject_Dispose(token);
return;
}
if ((CkJsonObject_JsonTypeOf(token,"access_token") != 1) || (CkJsonObject_StringOfEquals(token,"access_token","",TRUE) == TRUE)) {
printf("Expected a nonempty access_token in the JSON.\n");
CkJsonObject_Dispose(token);
return;
}
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) {
printf("Could not create the private operation directory.\n");
CkJsonObject_Dispose(token);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkFileAccess_Dispose(fac);
return;
}
body = CkJsonObject_Create();
CkJsonObject_putStrict(body,TRUE);
if (CkFileAccess_FileExists(fac,requestFile) == TRUE) {
success = CkJsonObject_LoadFile(body,requestFile);
if (success != TRUE) {
printf("Could not read the saved create request; nothing sent.\n");
CkJsonObject_Dispose(token);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkFileAccess_Dispose(fac);
CkJsonObject_Dispose(body);
return;
}
// Retry the same saved body and transactionId after an uncertain result.
}
else {
day = CkDateTime_Create();
success = CkDateTime_SetFromCurrentSystemTime(day);
if (success != TRUE) {
printf("Could not get the date.\n");
CkJsonObject_Dispose(token);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkFileAccess_Dispose(fac);
CkJsonObject_Dispose(body);
CkDateTime_Dispose(day);
return;
}
success = CkDateTime_AddDays(day,1);
if (success != TRUE) {
printf("Could not choose the start date.\n");
CkJsonObject_Dispose(token);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkFileAccess_Dispose(fac);
CkJsonObject_Dispose(body);
CkDateTime_Dispose(day);
return;
}
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) {
printf("Could not calculate the exclusive end date.\n");
CkJsonObject_Dispose(token);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkFileAccess_Dispose(fac);
CkJsonObject_Dispose(body);
CkDateTime_Dispose(day);
CkStringBuilder_Dispose(midnight);
return;
}
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) {
printf("Could not create a transaction ID.\n");
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);
return;
}
CkJsonObject_UpdateString(body,"transactionId",transactionId);
success = CkJsonObject_WriteFile(body,requestFile);
if (success != TRUE) {
printf("Could not save the request before sending; nothing sent.\n");
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);
return;
}
}
if ((CkJsonObject_JsonTypeOf(body,"transactionId") != 1) || (CkJsonObject_StringOfEquals(body,"transactionId","",TRUE) == TRUE)) {
printf("Expected a nonempty transactionId in the JSON.\n");
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);
return;
}
// A previous successful run leaves a small receipt. Do not submit it again.
if (CkFileAccess_FileExists(fac,resultFile) == TRUE) {
receipt = CkJsonObject_Create();
CkJsonObject_putStrict(receipt,TRUE);
success = CkJsonObject_LoadFile(receipt,resultFile);
if (success != TRUE) {
printf("Could not read the existing receipt; nothing sent.\n");
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);
return;
}
if ((CkJsonObject_JsonTypeOf(receipt,"id") != 1) || (CkJsonObject_StringOfEquals(receipt,"id","",TRUE) == TRUE)) {
printf("Expected a nonempty id in the JSON.\n");
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);
return;
}
if (CkJsonObject_StringOfEquals(receipt,"transactionId",CkJsonObject_stringOf(body,"transactionId"),TRUE) != TRUE) {
printf("The receipt belongs to another operation; nothing sent.\n");
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);
return;
}
printf("Already created event ID: %s\n",CkJsonObject_stringOf(receipt,"id"));
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);
return;
}
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) && (CkHttpResponse_getStatusCode(resp) == 0)) {
printf("No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying.\n");
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);
return;
}
if (CkHttpResponse_getStatusCode(resp) != 201) {
printf("Unexpected HTTP status: %d\n",CkHttpResponse_getStatusCode(resp));
printf("Request ID: %s\n",CkHttpResponse_getHeaderField(resp,"request-id"));
if (CkHttpResponse_getStatusCode(resp) == 429) {
printf("Retry-After: %s\n",CkHttpResponse_getHeaderField(resp,"Retry-After"));
}
if (CkHttpResponse_getStatusCode(resp) == 401) {
printf("Refresh the Graph access token or authorize again.\n");
}
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);
return;
}
success = CkJsonObject_Load(result,CkHttpResponse_bodyStr(resp));
if (success != TRUE) {
printf("Expected a well-formed JSON response.\n");
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);
return;
}
if ((CkJsonObject_JsonTypeOf(result,"id") != 1) || (CkJsonObject_StringOfEquals(result,"id","",TRUE) == TRUE)) {
printf("Expected a nonempty id in the JSON.\n");
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);
return;
}
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.
printf("Created event ID: %s\n",CkJsonObject_stringOf(result,"id"));
success = CkJsonObject_WriteFile(saved,resultFile);
if (success != TRUE) {
printf("The event was created, but its receipt could not be saved. Reconcile before retrying.\n");
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);
return;
}
printf("Start: %s (%s)\n",CkJsonObject_stringOf(result,"start.dateTime"),CkJsonObject_stringOf(result,"start.timeZone"));
printf("End: %s (%s)\n",CkJsonObject_stringOf(result,"end.dateTime"),CkJsonObject_stringOf(result,"end.timeZone"));
printf("Event receipt saved.\n");
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);
}