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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loToken
LOCAL loHttp
LOCAL loResp
LOCAL loResult
LOCAL lcAction
LOCAL lcCalendarId
LOCAL loBody
LOCAL loFac
LOCAL lcReceiptPath
LOCAL loOperation
LOCAL loEndpoint
lnSuccess = 0
* Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0
* Obtain a delegated Microsoft Graph token using the calendar authorization example.
* See Authorize Outlook Calendar with OAuth2 and PKCE.
loToken = CreateObject('Chilkat.JsonObject')
loToken.Strict = 1
lnSuccess = loToken.LoadFile("qa_data/tokens/outlookCalendar.json")
IF (lnSuccess <> 1) THEN
? "Could not read the private calendar token cache."
RELEASE loToken
CANCEL
ENDIF
IF ((loToken.JsonTypeOf("access_token") <> 1) OR (loToken.StringOfEquals("access_token","",1) = 1)) THEN
? "Expected a nonempty access_token in the JSON."
RELEASE loToken
CANCEL
ENDIF
loHttp = CreateObject('Chilkat.Http')
loHttp.AuthToken = loToken.StringOf("access_token")
loHttp.RequireSslCertVerify = 1
loHttp.FollowRedirects = 0
loHttp.ConnectTimeout = 20
loHttp.ReadTimeout = 30
loHttp.SetRequestHeader("Accept","application/json")
loResp = CreateObject('Chilkat.HttpResponse')
loResult = CreateObject('Chilkat.JsonObject')
loResult.Strict = 1
lcAction = "create"* Choose create, rename, or delete.
lcCalendarId = "YOUR_SECONDARY_CALENDAR_ID"
IF ((lcAction <> "create") AND (lcAction <> "rename") AND (lcAction <> "delete")) THEN
? "Choose create, rename, or delete."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
CANCEL
ENDIF
loBody = CreateObject('Chilkat.JsonObject')
loFac = CreateObject('Chilkat.FileAccess')
IF (lcAction = "create") THEN
lnSuccess = loFac.DirEnsureExists("qa_data/outlookCalendar")
IF (lnSuccess <> 1) THEN
? "Could not create the private operation directory."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loFac
CANCEL
ENDIF
lcReceiptPath = "qa_data/outlookCalendar/secondary-calendar.json"
loOperation = CreateObject('Chilkat.JsonObject')
loOperation.Strict = 1
IF (loFac.FileExists(lcReceiptPath) = 1) THEN
lnSuccess = loOperation.LoadFile(lcReceiptPath)
IF (lnSuccess <> 1) THEN
? "Could not read the previous operation; inspect it before retrying."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loFac
RELEASE loOperation
CANCEL
ENDIF
IF (loOperation.StringOfEquals("status","created",1) = 1) THEN
IF ((loOperation.JsonTypeOf("id") <> 1) OR (loOperation.StringOfEquals("id","",1) = 1)) THEN
? "Expected a nonempty id in the JSON."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loFac
RELEASE loOperation
CANCEL
ENDIF
? "Already created calendar ID: " + loOperation.StringOf("id")
ELSE
? "Creation may already have occurred. Reconcile the calendar list before starting another operation."
ENDIF
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loFac
RELEASE loOperation
CANCEL
ENDIF
loBody.UpdateString("name","Secondary calendar")
loOperation.UpdateString("status","pending")
loOperation.UpdateString("name",loBody.StringOf("name"))
* Calendar creation has no event transactionId. Record an attempt before sending.
lnSuccess = loOperation.WriteFile(lcReceiptPath)
IF (lnSuccess <> 1) THEN
? "Could not record the creation attempt; no request sent."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loFac
RELEASE loOperation
CANCEL
ENDIF
lnSuccess = loHttp.HttpJson("POST","https://graph.microsoft.com/v1.0/me/calendars",loBody,"application/json",loResp)
* A false result can still include an HTTP error response.
IF ((lnSuccess <> 1) AND (loResp.StatusCode = 0)) THEN
? "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loFac
RELEASE loOperation
CANCEL
ENDIF
IF (loResp.StatusCode <> 201) THEN
? "Unexpected HTTP status: " + STR(loResp.StatusCode)
? "Request ID: " + loResp.GetHeaderField("request-id")
IF (loResp.StatusCode = 429) THEN
? "Retry-After: " + loResp.GetHeaderField("Retry-After")
ENDIF
IF (loResp.StatusCode = 401) THEN
? "Refresh the Graph access token or authorize again."
ENDIF
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loFac
RELEASE loOperation
CANCEL
ENDIF
lnSuccess = loResult.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
? "Expected a well-formed JSON response."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loFac
RELEASE loOperation
CANCEL
ENDIF
IF ((loResult.JsonTypeOf("id") <> 1) OR (loResult.StringOfEquals("id","",1) = 1)) THEN
? "Expected a nonempty id in the JSON."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loFac
RELEASE loOperation
CANCEL
ENDIF
loOperation.UpdateString("id",loResult.StringOf("id"))
loOperation.UpdateString("status","created")
lnSuccess = loOperation.WriteFile(lcReceiptPath)
IF (lnSuccess <> 1) THEN
? "Could not save the receipt. Reconcile the pending attempt before retrying."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loFac
RELEASE loOperation
CANCEL
ENDIF
? "Created calendar ID: " + loResult.StringOf("id")
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loFac
RELEASE loOperation
CANCEL
ENDIF
* Use the saved ID for a deliberately selected secondary calendar.
loEndpoint = CreateObject('Chilkat.StringBuilder')
loEndpoint.Append("https://graph.microsoft.com/v1.0/me/calendars/")
loEndpoint.Append(loHttp.UrlEncode(lcCalendarId))
lnSuccess = loHttp.HttpNoBody("GET",loEndpoint.GetAsString(),loResp)
* A false result can still include an HTTP error response.
IF ((lnSuccess <> 1) AND (loResp.StatusCode = 0)) THEN
? "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loFac
RELEASE loOperation
RELEASE loEndpoint
CANCEL
ENDIF
IF (loResp.StatusCode <> 200) THEN
? "Unexpected HTTP status: " + STR(loResp.StatusCode)
? "Request ID: " + loResp.GetHeaderField("request-id")
IF (loResp.StatusCode = 429) THEN
? "Retry-After: " + loResp.GetHeaderField("Retry-After")
ENDIF
IF (loResp.StatusCode = 401) THEN
? "Refresh the Graph access token or authorize again."
ENDIF
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loFac
RELEASE loOperation
RELEASE loEndpoint
CANCEL
ENDIF
lnSuccess = loResult.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
? "Expected a well-formed JSON response."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loFac
RELEASE loOperation
RELEASE loEndpoint
CANCEL
ENDIF
IF ((loResult.JsonTypeOf("id") <> 1) OR (loResult.StringOfEquals("id","",1) = 1)) THEN
? "Expected a nonempty id in the JSON."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loFac
RELEASE loOperation
RELEASE loEndpoint
CANCEL
ENDIF
IF (loResult.JsonTypeOf("isDefaultCalendar") <> 5) THEN
? "Expected isDefaultCalendar in the calendar response."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loFac
RELEASE loOperation
RELEASE loEndpoint
CANCEL
ENDIF
IF (loResult.BoolOf("isDefaultCalendar") = 1) THEN
? "This example operates only on secondary calendars."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loFac
RELEASE loOperation
RELEASE loEndpoint
CANCEL
ENDIF
IF (lcAction = "rename") THEN
loBody.UpdateString("name","Renamed calendar")
lnSuccess = loHttp.HttpJson("PATCH",loEndpoint.GetAsString(),loBody,"application/json",loResp)
* A false result can still include an HTTP error response.
IF ((lnSuccess <> 1) AND (loResp.StatusCode = 0)) THEN
? "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loFac
RELEASE loOperation
RELEASE loEndpoint
CANCEL
ENDIF
IF (loResp.StatusCode <> 200) THEN
? "Unexpected HTTP status: " + STR(loResp.StatusCode)
? "Request ID: " + loResp.GetHeaderField("request-id")
IF (loResp.StatusCode = 429) THEN
? "Retry-After: " + loResp.GetHeaderField("Retry-After")
ENDIF
IF (loResp.StatusCode = 401) THEN
? "Refresh the Graph access token or authorize again."
ENDIF
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loFac
RELEASE loOperation
RELEASE loEndpoint
CANCEL
ENDIF
lnSuccess = loResult.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
? "Expected a well-formed JSON response."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loFac
RELEASE loOperation
RELEASE loEndpoint
CANCEL
ENDIF
IF ((loResult.JsonTypeOf("id") <> 1) OR (loResult.StringOfEquals("id","",1) = 1)) THEN
? "Expected a nonempty id in the JSON."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loFac
RELEASE loOperation
RELEASE loEndpoint
CANCEL
ENDIF
? "Renamed calendar ID: " + loResult.StringOf("id")
ELSE
* Live deletion removes this secondary calendar and its events.
lnSuccess = loHttp.HttpNoBody("DELETE",loEndpoint.GetAsString(),loResp)
* A false result can still include an HTTP error response.
IF ((lnSuccess <> 1) AND (loResp.StatusCode = 0)) THEN
? "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying."
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loFac
RELEASE loOperation
RELEASE loEndpoint
CANCEL
ENDIF
IF (loResp.StatusCode <> 204) THEN
? "Unexpected HTTP status: " + STR(loResp.StatusCode)
? "Request ID: " + loResp.GetHeaderField("request-id")
IF (loResp.StatusCode = 429) THEN
? "Retry-After: " + loResp.GetHeaderField("Retry-After")
ENDIF
IF (loResp.StatusCode = 401) THEN
? "Refresh the Graph access token or authorize again."
ENDIF
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loFac
RELEASE loOperation
RELEASE loEndpoint
CANCEL
ENDIF
? "Secondary calendar deleted (HTTP 204)."
ENDIF
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loResult
RELEASE loBody
RELEASE loFac
RELEASE loOperation
RELEASE loEndpoint