Outlook Calendar: Create, Rename, or Delete a Secondary Calendar
See more Outlook Calendar Examples
Select one operation: create (the default), rename, or delete. Creation returns an opaque calendar ID to save with the application record. To rename or delete, explicitly set that ID and change action. The example retrieves the calendar first and refuses to operate on the default calendar. Names are display labels and are not unique identifiers.
Live changes: rename changes the calendar name; delete removes the secondary calendar and its events. The operations are alternatives, not a create/rename/delete sequence. Never delete a calendar merely to clean up an example without checking its contents and ownership. Deletion succeeds with HTTP 204 and no JSON body.
Unlike event creation, calendar creation has no transactionId deduplication property. A private pending record is saved before sending; an uncertain outcome or failed receipt write blocks automatic repeat creation. Inspect the calendar list and reconcile that attempt first. Keep operation files per account, with a single writer. Use a new filename only for a deliberately new calendar. A completed receipt prevents accidental creation when rerunning the sample.
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 AutoIt Downloads
Local $bSuccess = False
; Assumes Chilkat is unlocked. See Global Unlock Sample.
$bSuccess = False
; Obtain a delegated Microsoft Graph token using the calendar authorization example.
; See Authorize Outlook Calendar with OAuth2 and PKCE.
$oToken = ObjCreate("Chilkat.JsonObject")
$oToken.Strict = True
$bSuccess = $oToken.LoadFile("qa_data/tokens/outlookCalendar.json")
If ($bSuccess <> True) Then
ConsoleWrite("Could not read the private calendar token cache." & @CRLF)
Exit
EndIf
If (($oToken.JsonTypeOf("access_token") <> 1) Or ($oToken.StringOfEquals("access_token","",True) = True)) Then
ConsoleWrite("Expected a nonempty access_token in the JSON." & @CRLF)
Exit
EndIf
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.AuthToken = $oToken.StringOf("access_token")
$oHttp.RequireSslCertVerify = True
$oHttp.FollowRedirects = False
$oHttp.ConnectTimeout = 20
$oHttp.ReadTimeout = 30
$oHttp.SetRequestHeader "Accept","application/json"
$oResp = ObjCreate("Chilkat.HttpResponse")
$oResult = ObjCreate("Chilkat.JsonObject")
$oResult.Strict = True
Local $sAction = "create"; Choose create, rename, or delete.
Local $sCalendarId = "YOUR_SECONDARY_CALENDAR_ID"
If (($sAction <> "create") And ($sAction <> "rename") And ($sAction <> "delete")) Then
ConsoleWrite("Choose create, rename, or delete." & @CRLF)
Exit
EndIf
$oBody = ObjCreate("Chilkat.JsonObject")
$oFac = ObjCreate("Chilkat.FileAccess")
If ($sAction = "create") Then
$bSuccess = $oFac.DirEnsureExists("qa_data/outlookCalendar")
If ($bSuccess <> True) Then
ConsoleWrite("Could not create the private operation directory." & @CRLF)
Exit
EndIf
Local $sReceiptPath = "qa_data/outlookCalendar/secondary-calendar.json"
$oOperation = ObjCreate("Chilkat.JsonObject")
$oOperation.Strict = True
If ($oFac.FileExists($sReceiptPath) = True) Then
$bSuccess = $oOperation.LoadFile($sReceiptPath)
If ($bSuccess <> True) Then
ConsoleWrite("Could not read the previous operation; inspect it before retrying." & @CRLF)
Exit
EndIf
If ($oOperation.StringOfEquals("status","created",True) = True) Then
If (($oOperation.JsonTypeOf("id") <> 1) Or ($oOperation.StringOfEquals("id","",True) = True)) Then
ConsoleWrite("Expected a nonempty id in the JSON." & @CRLF)
Exit
EndIf
ConsoleWrite("Already created calendar ID: " & $oOperation.StringOf("id") & @CRLF)
Else
ConsoleWrite("Creation may already have occurred. Reconcile the calendar list before starting another operation." & @CRLF)
EndIf
Exit
EndIf
$oBody.UpdateString("name","Secondary calendar")
$oOperation.UpdateString("status","pending")
$oOperation.UpdateString("name",$oBody.StringOf("name"))
; Calendar creation has no event transactionId. Record an attempt before sending.
$bSuccess = $oOperation.WriteFile($sReceiptPath)
If ($bSuccess <> True) Then
ConsoleWrite("Could not record the creation attempt; no request sent." & @CRLF)
Exit
EndIf
$bSuccess = $oHttp.HttpJson("POST","https://graph.microsoft.com/v1.0/me/calendars",$oBody,"application/json",$oResp)
; A false result can still include an HTTP error response.
If (($bSuccess <> True) And ($oResp.StatusCode = 0)) Then
ConsoleWrite("No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying." & @CRLF)
Exit
EndIf
If ($oResp.StatusCode <> 201) Then
ConsoleWrite("Unexpected HTTP status: " & $oResp.StatusCode & @CRLF)
ConsoleWrite("Request ID: " & $oResp.GetHeaderField("request-id") & @CRLF)
If ($oResp.StatusCode = 429) Then
ConsoleWrite("Retry-After: " & $oResp.GetHeaderField("Retry-After") & @CRLF)
EndIf
If ($oResp.StatusCode = 401) Then
ConsoleWrite("Refresh the Graph access token or authorize again." & @CRLF)
EndIf
Exit
EndIf
$bSuccess = $oResult.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Expected a well-formed JSON response." & @CRLF)
Exit
EndIf
If (($oResult.JsonTypeOf("id") <> 1) Or ($oResult.StringOfEquals("id","",True) = True)) Then
ConsoleWrite("Expected a nonempty id in the JSON." & @CRLF)
Exit
EndIf
$oOperation.UpdateString("id",$oResult.StringOf("id"))
$oOperation.UpdateString("status","created")
$bSuccess = $oOperation.WriteFile($sReceiptPath)
If ($bSuccess <> True) Then
ConsoleWrite("Could not save the receipt. Reconcile the pending attempt before retrying." & @CRLF)
Exit
EndIf
ConsoleWrite("Created calendar ID: " & $oResult.StringOf("id") & @CRLF)
Exit
EndIf
; Use the saved ID for a deliberately selected secondary calendar.
$oEndpoint = ObjCreate("Chilkat.StringBuilder")
$oEndpoint.Append("https://graph.microsoft.com/v1.0/me/calendars/")
$oEndpoint.Append($oHttp.UrlEncode($sCalendarId))
$bSuccess = $oHttp.HttpNoBody("GET",$oEndpoint.GetAsString(),$oResp)
; A false result can still include an HTTP error response.
If (($bSuccess <> True) And ($oResp.StatusCode = 0)) Then
ConsoleWrite("No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying." & @CRLF)
Exit
EndIf
If ($oResp.StatusCode <> 200) Then
ConsoleWrite("Unexpected HTTP status: " & $oResp.StatusCode & @CRLF)
ConsoleWrite("Request ID: " & $oResp.GetHeaderField("request-id") & @CRLF)
If ($oResp.StatusCode = 429) Then
ConsoleWrite("Retry-After: " & $oResp.GetHeaderField("Retry-After") & @CRLF)
EndIf
If ($oResp.StatusCode = 401) Then
ConsoleWrite("Refresh the Graph access token or authorize again." & @CRLF)
EndIf
Exit
EndIf
$bSuccess = $oResult.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Expected a well-formed JSON response." & @CRLF)
Exit
EndIf
If (($oResult.JsonTypeOf("id") <> 1) Or ($oResult.StringOfEquals("id","",True) = True)) Then
ConsoleWrite("Expected a nonempty id in the JSON." & @CRLF)
Exit
EndIf
If ($oResult.JsonTypeOf("isDefaultCalendar") <> 5) Then
ConsoleWrite("Expected isDefaultCalendar in the calendar response." & @CRLF)
Exit
EndIf
If ($oResult.BoolOf("isDefaultCalendar") = True) Then
ConsoleWrite("This example operates only on secondary calendars." & @CRLF)
Exit
EndIf
If ($sAction = "rename") Then
$oBody.UpdateString("name","Renamed calendar")
$bSuccess = $oHttp.HttpJson("PATCH",$oEndpoint.GetAsString(),$oBody,"application/json",$oResp)
; A false result can still include an HTTP error response.
If (($bSuccess <> True) And ($oResp.StatusCode = 0)) Then
ConsoleWrite("No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying." & @CRLF)
Exit
EndIf
If ($oResp.StatusCode <> 200) Then
ConsoleWrite("Unexpected HTTP status: " & $oResp.StatusCode & @CRLF)
ConsoleWrite("Request ID: " & $oResp.GetHeaderField("request-id") & @CRLF)
If ($oResp.StatusCode = 429) Then
ConsoleWrite("Retry-After: " & $oResp.GetHeaderField("Retry-After") & @CRLF)
EndIf
If ($oResp.StatusCode = 401) Then
ConsoleWrite("Refresh the Graph access token or authorize again." & @CRLF)
EndIf
Exit
EndIf
$bSuccess = $oResult.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Expected a well-formed JSON response." & @CRLF)
Exit
EndIf
If (($oResult.JsonTypeOf("id") <> 1) Or ($oResult.StringOfEquals("id","",True) = True)) Then
ConsoleWrite("Expected a nonempty id in the JSON." & @CRLF)
Exit
EndIf
ConsoleWrite("Renamed calendar ID: " & $oResult.StringOf("id") & @CRLF)
Else
; Live deletion removes this secondary calendar and its events.
$bSuccess = $oHttp.HttpNoBody("DELETE",$oEndpoint.GetAsString(),$oResp)
; A false result can still include an HTTP error response.
If (($bSuccess <> True) And ($oResp.StatusCode = 0)) Then
ConsoleWrite("No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying." & @CRLF)
Exit
EndIf
If ($oResp.StatusCode <> 204) Then
ConsoleWrite("Unexpected HTTP status: " & $oResp.StatusCode & @CRLF)
ConsoleWrite("Request ID: " & $oResp.GetHeaderField("request-id") & @CRLF)
If ($oResp.StatusCode = 429) Then
ConsoleWrite("Retry-After: " & $oResp.GetHeaderField("Retry-After") & @CRLF)
EndIf
If ($oResp.StatusCode = 401) Then
ConsoleWrite("Refresh the Graph access token or authorize again." & @CRLF)
EndIf
Exit
EndIf
ConsoleWrite("Secondary calendar deleted (HTTP 204)." & @CRLF)
EndIf