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 C Downloads
#include <C_CkJsonObject.h>
#include <C_CkHttp.h>
#include <C_CkHttpResponse.h>
#include <C_CkFileAccess.h>
#include <C_CkDateTime.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;
BOOL enableTeams;
HCkJsonObject body;
BOOL teamsAllowed;
int providerIndex;
HCkDateTime when;
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 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 = "qa_data/outlookCalendar/create-meeting-request.json";
resultFile = "qa_data/outlookCalendar/created-meeting.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;
}
// Set true only for a Teams-enabled organizational calendar.
enableTeams = FALSE;
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 {
if (enableTeams == TRUE) {
success = CkHttp_HttpNoBody(http,"GET","https://graph.microsoft.com/v1.0/me/calendar?$select=allowedOnlineMeetingProviders",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);
return;
}
if (CkHttpResponse_getStatusCode(resp) != 200) {
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);
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);
return;
}
if (CkJsonObject_JsonTypeOf(result,"allowedOnlineMeetingProviders") != 4) {
printf("Could not determine supported meeting providers.\n");
CkJsonObject_Dispose(token);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkFileAccess_Dispose(fac);
CkJsonObject_Dispose(body);
return;
}
teamsAllowed = FALSE;
providerIndex = 0;
while ((providerIndex < CkJsonObject_SizeOfArray(result,"allowedOnlineMeetingProviders"))) {
CkJsonObject_putI(result,providerIndex);
if (CkJsonObject_StringOfEquals(result,"allowedOnlineMeetingProviders[i]","teamsForBusiness",TRUE) == TRUE) {
teamsAllowed = TRUE;
}
providerIndex = providerIndex + 1;
}
if (teamsAllowed != TRUE) {
printf("This calendar does not list Teams as an allowed provider; nothing created.\n");
CkJsonObject_Dispose(token);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkFileAccess_Dispose(fac);
CkJsonObject_Dispose(body);
return;
}
CkJsonObject_UpdateBool(body,"isOnlineMeeting",TRUE);
CkJsonObject_UpdateString(body,"onlineMeetingProvider","teamsForBusiness");
}
when = CkDateTime_Create();
success = CkDateTime_SetFromCurrentSystemTime(when);
if (success != TRUE) {
printf("Could not get the time.\n");
CkJsonObject_Dispose(token);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkFileAccess_Dispose(fac);
CkJsonObject_Dispose(body);
CkDateTime_Dispose(when);
return;
}
success = CkDateTime_AddDays(when,1);
if (success != TRUE) {
printf("Could not calculate tomorrow.\n");
CkJsonObject_Dispose(token);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkFileAccess_Dispose(fac);
CkJsonObject_Dispose(body);
CkDateTime_Dispose(when);
return;
}
CkJsonObject_UpdateString(body,"subject","Sample meeting");
CkJsonObject_UpdateString(body,"body.contentType","text");
CkJsonObject_UpdateString(body,"body.content","Sample event details.");
CkJsonObject_UpdateString(body,"start.dateTime",CkDateTime_getAsIso8601(when,"YYYY-MM-DDThh:mm:ss",FALSE));
CkJsonObject_UpdateString(body,"start.timeZone","UTC");
success = CkDateTime_AddSeconds(when,3600);
if (success != TRUE) {
printf("Could not calculate the end.\n");
CkJsonObject_Dispose(token);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkFileAccess_Dispose(fac);
CkJsonObject_Dispose(body);
CkDateTime_Dispose(when);
return;
}
CkJsonObject_UpdateString(body,"end.dateTime",CkDateTime_getAsIso8601(when,"YYYY-MM-DDThh:mm:ss",FALSE));
CkJsonObject_UpdateString(body,"end.timeZone","UTC");
CkJsonObject_UpdateString(body,"attendees[0].emailAddress.address","attendee@example.com");
CkJsonObject_UpdateString(body,"attendees[0].type","required");
CkJsonObject_UpdateBool(body,"responseRequested",TRUE);
// Adding attendees sends real invitations. Replace the sample address before running.
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(when);
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(when);
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(when);
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(when);
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(when);
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(when);
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(when);
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(when);
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(when);
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(when);
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(when);
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(when);
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");
if (CkJsonObject_JsonTypeOf(result,"onlineMeeting.joinUrl") == 1) {
printf("Meeting join URL: %s\n",CkJsonObject_stringOf(result,"onlineMeeting.joinUrl"));
}
CkJsonObject_Dispose(token);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkFileAccess_Dispose(fac);
CkJsonObject_Dispose(body);
CkDateTime_Dispose(when);
CkCrypt2_Dispose(crypt);
CkJsonObject_Dispose(receipt);
CkJsonObject_Dispose(saved);
}