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 Go Downloads
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 := chilkat.NewJsonObject()
token.SetStrict(true)
success = token.LoadFile("qa_data/tokens/outlookCalendar.json")
if success != true {
fmt.Println("Could not read the private calendar token cache.")
token.DisposeJsonObject()
return
}
if (token.JsonTypeOf("access_token") != 1) || (token.StringOfEquals("access_token","",true) == true) {
fmt.Println("Expected a nonempty access_token in the JSON.")
token.DisposeJsonObject()
return
}
http := chilkat.NewHttp()
http.SetAuthToken(token.StringOf("access_token"))
http.SetRequireSslCertVerify(true)
http.SetFollowRedirects(false)
http.SetConnectTimeout(20)
http.SetReadTimeout(30)
http.SetRequestHeader("Accept","application/json")
resp := chilkat.NewHttpResponse()
result := chilkat.NewJsonObject()
result.SetStrict(true)
action := "create" // Choose create, rename, or delete.
calendarId := "YOUR_SECONDARY_CALENDAR_ID"
if (action != "create") && (action != "rename") && (action != "delete") {
fmt.Println("Choose create, rename, or delete.")
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
result.DisposeJsonObject()
return
}
body := chilkat.NewJsonObject()
fac := chilkat.NewFileAccess()
if action == "create" {
success = fac.DirEnsureExists("qa_data/outlookCalendar")
if success != true {
fmt.Println("Could not create the private operation directory.")
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
result.DisposeJsonObject()
body.DisposeJsonObject()
fac.DisposeFileAccess()
return
}
receiptPath := "qa_data/outlookCalendar/secondary-calendar.json"
operation := chilkat.NewJsonObject()
operation.SetStrict(true)
if fac.FileExists(receiptPath) == true {
success = operation.LoadFile(receiptPath)
if success != true {
fmt.Println("Could not read the previous operation; inspect it before retrying.")
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
result.DisposeJsonObject()
body.DisposeJsonObject()
fac.DisposeFileAccess()
operation.DisposeJsonObject()
return
}
if operation.StringOfEquals("status","created",true) == true {
if (operation.JsonTypeOf("id") != 1) || (operation.StringOfEquals("id","",true) == true) {
fmt.Println("Expected a nonempty id in the JSON.")
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
result.DisposeJsonObject()
body.DisposeJsonObject()
fac.DisposeFileAccess()
operation.DisposeJsonObject()
return
}
fmt.Println("Already created calendar ID: ", *operation.StringOf("id"))
} else {
fmt.Println("Creation may already have occurred. Reconcile the calendar list before starting another operation.")
}
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
result.DisposeJsonObject()
body.DisposeJsonObject()
fac.DisposeFileAccess()
operation.DisposeJsonObject()
return
}
body.UpdateString("name","Secondary calendar")
operation.UpdateString("status","pending")
operation.UpdateString("name",*body.StringOf("name"))
// Calendar creation has no event transactionId. Record an attempt before sending.
success = operation.WriteFile(receiptPath)
if success != true {
fmt.Println("Could not record the creation attempt; no request sent.")
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
result.DisposeJsonObject()
body.DisposeJsonObject()
fac.DisposeFileAccess()
operation.DisposeJsonObject()
return
}
success = http.HttpJson("POST","https://graph.microsoft.com/v1.0/me/calendars",body,"application/json",resp)
// A false result can still include an HTTP error response.
if (success != true) && (resp.StatusCode() == 0) {
fmt.Println("No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying.")
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
result.DisposeJsonObject()
body.DisposeJsonObject()
fac.DisposeFileAccess()
operation.DisposeJsonObject()
return
}
if resp.StatusCode() != 201 {
fmt.Println("Unexpected HTTP status: ", resp.StatusCode())
fmt.Println("Request ID: ", *resp.GetHeaderField("request-id"))
if resp.StatusCode() == 429 {
fmt.Println("Retry-After: ", *resp.GetHeaderField("Retry-After"))
}
if resp.StatusCode() == 401 {
fmt.Println("Refresh the Graph access token or authorize again.")
}
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
result.DisposeJsonObject()
body.DisposeJsonObject()
fac.DisposeFileAccess()
operation.DisposeJsonObject()
return
}
success = result.Load(resp.BodyStr())
if success != true {
fmt.Println("Expected a well-formed JSON response.")
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
result.DisposeJsonObject()
body.DisposeJsonObject()
fac.DisposeFileAccess()
operation.DisposeJsonObject()
return
}
if (result.JsonTypeOf("id") != 1) || (result.StringOfEquals("id","",true) == true) {
fmt.Println("Expected a nonempty id in the JSON.")
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
result.DisposeJsonObject()
body.DisposeJsonObject()
fac.DisposeFileAccess()
operation.DisposeJsonObject()
return
}
operation.UpdateString("id",*result.StringOf("id"))
operation.UpdateString("status","created")
success = operation.WriteFile(receiptPath)
if success != true {
fmt.Println("Could not save the receipt. Reconcile the pending attempt before retrying.")
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
result.DisposeJsonObject()
body.DisposeJsonObject()
fac.DisposeFileAccess()
operation.DisposeJsonObject()
return
}
fmt.Println("Created calendar ID: ", *result.StringOf("id"))
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
result.DisposeJsonObject()
body.DisposeJsonObject()
fac.DisposeFileAccess()
operation.DisposeJsonObject()
return
}
// Use the saved ID for a deliberately selected secondary calendar.
endpoint := chilkat.NewStringBuilder()
endpoint.Append("https://graph.microsoft.com/v1.0/me/calendars/")
endpoint.Append(*http.UrlEncode(calendarId))
success = http.HttpNoBody("GET",*endpoint.GetAsString(),resp)
// A false result can still include an HTTP error response.
if (success != true) && (resp.StatusCode() == 0) {
fmt.Println("No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying.")
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
result.DisposeJsonObject()
body.DisposeJsonObject()
fac.DisposeFileAccess()
operation.DisposeJsonObject()
endpoint.DisposeStringBuilder()
return
}
if resp.StatusCode() != 200 {
fmt.Println("Unexpected HTTP status: ", resp.StatusCode())
fmt.Println("Request ID: ", *resp.GetHeaderField("request-id"))
if resp.StatusCode() == 429 {
fmt.Println("Retry-After: ", *resp.GetHeaderField("Retry-After"))
}
if resp.StatusCode() == 401 {
fmt.Println("Refresh the Graph access token or authorize again.")
}
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
result.DisposeJsonObject()
body.DisposeJsonObject()
fac.DisposeFileAccess()
operation.DisposeJsonObject()
endpoint.DisposeStringBuilder()
return
}
success = result.Load(resp.BodyStr())
if success != true {
fmt.Println("Expected a well-formed JSON response.")
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
result.DisposeJsonObject()
body.DisposeJsonObject()
fac.DisposeFileAccess()
operation.DisposeJsonObject()
endpoint.DisposeStringBuilder()
return
}
if (result.JsonTypeOf("id") != 1) || (result.StringOfEquals("id","",true) == true) {
fmt.Println("Expected a nonempty id in the JSON.")
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
result.DisposeJsonObject()
body.DisposeJsonObject()
fac.DisposeFileAccess()
operation.DisposeJsonObject()
endpoint.DisposeStringBuilder()
return
}
if result.JsonTypeOf("isDefaultCalendar") != 5 {
fmt.Println("Expected isDefaultCalendar in the calendar response.")
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
result.DisposeJsonObject()
body.DisposeJsonObject()
fac.DisposeFileAccess()
operation.DisposeJsonObject()
endpoint.DisposeStringBuilder()
return
}
if result.BoolOf("isDefaultCalendar") == true {
fmt.Println("This example operates only on secondary calendars.")
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
result.DisposeJsonObject()
body.DisposeJsonObject()
fac.DisposeFileAccess()
operation.DisposeJsonObject()
endpoint.DisposeStringBuilder()
return
}
if action == "rename" {
body.UpdateString("name","Renamed calendar")
success = http.HttpJson("PATCH",*endpoint.GetAsString(),body,"application/json",resp)
// A false result can still include an HTTP error response.
if (success != true) && (resp.StatusCode() == 0) {
fmt.Println("No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying.")
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
result.DisposeJsonObject()
body.DisposeJsonObject()
fac.DisposeFileAccess()
operation.DisposeJsonObject()
endpoint.DisposeStringBuilder()
return
}
if resp.StatusCode() != 200 {
fmt.Println("Unexpected HTTP status: ", resp.StatusCode())
fmt.Println("Request ID: ", *resp.GetHeaderField("request-id"))
if resp.StatusCode() == 429 {
fmt.Println("Retry-After: ", *resp.GetHeaderField("Retry-After"))
}
if resp.StatusCode() == 401 {
fmt.Println("Refresh the Graph access token or authorize again.")
}
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
result.DisposeJsonObject()
body.DisposeJsonObject()
fac.DisposeFileAccess()
operation.DisposeJsonObject()
endpoint.DisposeStringBuilder()
return
}
success = result.Load(resp.BodyStr())
if success != true {
fmt.Println("Expected a well-formed JSON response.")
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
result.DisposeJsonObject()
body.DisposeJsonObject()
fac.DisposeFileAccess()
operation.DisposeJsonObject()
endpoint.DisposeStringBuilder()
return
}
if (result.JsonTypeOf("id") != 1) || (result.StringOfEquals("id","",true) == true) {
fmt.Println("Expected a nonempty id in the JSON.")
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
result.DisposeJsonObject()
body.DisposeJsonObject()
fac.DisposeFileAccess()
operation.DisposeJsonObject()
endpoint.DisposeStringBuilder()
return
}
fmt.Println("Renamed calendar ID: ", *result.StringOf("id"))
} else {
// Live deletion removes this secondary calendar and its events.
success = http.HttpNoBody("DELETE",*endpoint.GetAsString(),resp)
// A false result can still include an HTTP error response.
if (success != true) && (resp.StatusCode() == 0) {
fmt.Println("No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying.")
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
result.DisposeJsonObject()
body.DisposeJsonObject()
fac.DisposeFileAccess()
operation.DisposeJsonObject()
endpoint.DisposeStringBuilder()
return
}
if resp.StatusCode() != 204 {
fmt.Println("Unexpected HTTP status: ", resp.StatusCode())
fmt.Println("Request ID: ", *resp.GetHeaderField("request-id"))
if resp.StatusCode() == 429 {
fmt.Println("Retry-After: ", *resp.GetHeaderField("Retry-After"))
}
if resp.StatusCode() == 401 {
fmt.Println("Refresh the Graph access token or authorize again.")
}
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
result.DisposeJsonObject()
body.DisposeJsonObject()
fac.DisposeFileAccess()
operation.DisposeJsonObject()
endpoint.DisposeStringBuilder()
return
}
fmt.Println("Secondary calendar deleted (HTTP 204).")
}
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
result.DisposeJsonObject()
body.DisposeJsonObject()
fac.DisposeFileAccess()
operation.DisposeJsonObject()
endpoint.DisposeStringBuilder()