Outlook Calendar: Create a Meeting with Attendees and Optional Teams
See more Outlook Calendar Examples
Sends invitations: replace attendee@example.com with an intended recipient before running. Graph sends invitations when attendees are supplied. This example creates a one-hour meeting tomorrow and saves its ID. The responseRequested property asks attendees for responses; it is not a switch to suppress the invitations.
Set enableTeams=true for a supported Microsoft 365 calendar. The example checks allowedOnlineMeetingProviders before creating a new request, sets isOnlineMeeting and teamsForBusiness, and reads onlineMeeting.joinUrl. This uses the calendar API, not the standalone onlineMeetings API. With enableTeams=false it creates an ordinary meeting. A persisted request retains its original settings on retry; do not change settings underneath the same transaction ID.
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_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;
BOOL enableTeams;
HCkJsonObjectW body;
BOOL teamsAllowed;
int providerIndex;
HCkDateTimeW when;
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 a meeting and sends invitations 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-meeting-request.json";
resultFile = L"qa_data/outlookCalendar/created-meeting.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;
}
// Set true only for a Teams-enabled organizational calendar.
enableTeams = FALSE;
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 {
if (enableTeams == TRUE) {
success = CkHttpW_HttpNoBody(http,L"GET",L"https://graph.microsoft.com/v1.0/me/calendar?$select=allowedOnlineMeetingProviders",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);
return;
}
if (CkHttpResponseW_getStatusCode(resp) != 200) {
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);
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);
return;
}
if (CkJsonObjectW_JsonTypeOf(result,L"allowedOnlineMeetingProviders") != 4) {
wprintf(L"Could not determine supported meeting providers.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(result);
CkFileAccessW_Dispose(fac);
CkJsonObjectW_Dispose(body);
return;
}
teamsAllowed = FALSE;
providerIndex = 0;
while ((providerIndex < CkJsonObjectW_SizeOfArray(result,L"allowedOnlineMeetingProviders"))) {
CkJsonObjectW_putI(result,providerIndex);
if (CkJsonObjectW_StringOfEquals(result,L"allowedOnlineMeetingProviders[i]",L"teamsForBusiness",TRUE) == TRUE) {
teamsAllowed = TRUE;
}
providerIndex = providerIndex + 1;
}
if (teamsAllowed != TRUE) {
wprintf(L"This calendar does not list Teams as an allowed provider; nothing created.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(result);
CkFileAccessW_Dispose(fac);
CkJsonObjectW_Dispose(body);
return;
}
CkJsonObjectW_UpdateBool(body,L"isOnlineMeeting",TRUE);
CkJsonObjectW_UpdateString(body,L"onlineMeetingProvider",L"teamsForBusiness");
}
when = CkDateTimeW_Create();
success = CkDateTimeW_SetFromCurrentSystemTime(when);
if (success != TRUE) {
wprintf(L"Could not get the time.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(result);
CkFileAccessW_Dispose(fac);
CkJsonObjectW_Dispose(body);
CkDateTimeW_Dispose(when);
return;
}
success = CkDateTimeW_AddDays(when,1);
if (success != TRUE) {
wprintf(L"Could not calculate tomorrow.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(result);
CkFileAccessW_Dispose(fac);
CkJsonObjectW_Dispose(body);
CkDateTimeW_Dispose(when);
return;
}
CkJsonObjectW_UpdateString(body,L"subject",L"Sample meeting");
CkJsonObjectW_UpdateString(body,L"body.contentType",L"text");
CkJsonObjectW_UpdateString(body,L"body.content",L"Sample event details.");
CkJsonObjectW_UpdateString(body,L"start.dateTime",CkDateTimeW_getAsIso8601(when,L"YYYY-MM-DDThh:mm:ss",FALSE));
CkJsonObjectW_UpdateString(body,L"start.timeZone",L"UTC");
success = CkDateTimeW_AddSeconds(when,3600);
if (success != TRUE) {
wprintf(L"Could not calculate the end.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(result);
CkFileAccessW_Dispose(fac);
CkJsonObjectW_Dispose(body);
CkDateTimeW_Dispose(when);
return;
}
CkJsonObjectW_UpdateString(body,L"end.dateTime",CkDateTimeW_getAsIso8601(when,L"YYYY-MM-DDThh:mm:ss",FALSE));
CkJsonObjectW_UpdateString(body,L"end.timeZone",L"UTC");
CkJsonObjectW_UpdateString(body,L"attendees[0].emailAddress.address",L"attendee@example.com");
CkJsonObjectW_UpdateString(body,L"attendees[0].type",L"required");
CkJsonObjectW_UpdateBool(body,L"responseRequested",TRUE);
// Adding attendees sends real invitations. Replace the sample address before running.
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(when);
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(when);
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(when);
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(when);
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(when);
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(when);
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(when);
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(when);
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(when);
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(when);
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(when);
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(when);
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");
if (CkJsonObjectW_JsonTypeOf(result,L"onlineMeeting.joinUrl") == 1) {
wprintf(L"Meeting join URL: %s\n",CkJsonObjectW_stringOf(result,L"onlineMeeting.joinUrl"));
}
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(result);
CkFileAccessW_Dispose(fac);
CkJsonObjectW_Dispose(body);
CkDateTimeW_Dispose(when);
CkCrypt2W_Dispose(crypt);
CkJsonObjectW_Dispose(receipt);
CkJsonObjectW_Dispose(saved);
}