Sample code for 30+ languages & platforms
Tcl

Outlook Calendar Create an Event

See more Outlook Calendar Examples

Create an event in the specified time zone, and assign the event an optional transactionId value.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

# This example requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

set http [new_CkHttp]

# Use your previously obtained access token here: Get Outlook Calendar OAuth2 Access Token (Azure AD v2.0 Endpoint).

set jsonToken [new_CkJsonObject]

set success [CkJsonObject_LoadFile $jsonToken "qa_data/tokens/outlookCalendar.json"]
if {$success == 0} then {
    puts [CkJsonObject_lastErrorText $jsonToken]
    delete_CkHttp $http
    delete_CkJsonObject $jsonToken
    exit
}

CkHttp_put_AuthToken $http [CkJsonObject_stringOf $jsonToken "access_token"]

# Send the following POST:

# POST https://graph.microsoft.com/v1.0/me/events
# Prefer: outlook.timezone="Pacific Standard Time"
# Content-type: application/json
# 
# {
#   "subject": "Let's go for lunch",
#   "body": {
#     "contentType": "HTML",
#     "content": "Does noon work for you?"
#   },
#   "start": {
#       "dateTime": "2017-04-15T12:00:00",
#       "timeZone": "Pacific Standard Time"
#   },
#   "end": {
#       "dateTime": "2017-04-15T14:00:00",
#       "timeZone": "Pacific Standard Time"
#   },
#   "location":{
#       "displayName":"Harry's Bar"
#   },
#   "attendees": [
#     {
#       "emailAddress": {
#         "address":"samanthab@contoso.onmicrosoft.com",
#         "name": "Samantha Booth"
#       },
#       "type": "required"
#     }
#   ],
#   "allowNewTimeProposals": true,
#   "transactionId":"7E163156-7762-4BEB-A1C6-729EA81755A7"
# }

# Build the JSON body of the POST.
set json [new_CkJsonObject]

CkJsonObject_UpdateString $json "subject" "Let's go for lunch"
CkJsonObject_UpdateString $json "body.contentType" "HTML"
CkJsonObject_UpdateString $json "body.content" "Does noon work for you?"
CkJsonObject_UpdateString $json "start.dateTime" "2021-05-15T12:00:00"
CkJsonObject_UpdateString $json "start.timeZone" "Pacific Standard Time"
CkJsonObject_UpdateString $json "end.dateTime" "2021-05-15T14:00:00"
CkJsonObject_UpdateString $json "end.timeZone" "Pacific Standard Time"
CkJsonObject_UpdateString $json "location.displayName" "Harry's Bar"
CkJsonObject_UpdateString $json "attendees[0].emailAddress.address" "samanthab@contoso.onmicrosoft.com"
CkJsonObject_UpdateString $json "attendees[0].emailAddress.name" "Samantha Booth"
CkJsonObject_UpdateString $json "attendees[0].type" "required"
CkJsonObject_UpdateBool $json "allowNewTimeProposals" 1

# Generate a UUID.
set crypt [new_CkCrypt2]

CkJsonObject_UpdateString $json "transactionId" [CkCrypt2_generateUuid $crypt]

# Add the "Prefer" request header.
CkHttp_SetRequestHeader $http "Prefer" "outlook.timezone=\"Pacific Standard Time\""

# Send the HTTP POST
set resp [new_CkHttpResponse]

set success [CkHttp_HttpJson $http "POST" "https://graph.microsoft.com/v1.0/me/events" $json "application/json" $resp]
if {$success == 0} then {
    puts [CkHttp_lastErrorText $http]
    delete_CkHttp $http
    delete_CkJsonObject $jsonToken
    delete_CkJsonObject $json
    delete_CkCrypt2 $crypt
    delete_CkHttpResponse $resp
    exit
}

puts "Response status code = [CkHttpResponse_get_StatusCode $resp]"

set jResp [new_CkJsonObject]

CkJsonObject_Load $jResp [CkHttpResponse_bodyStr $resp]
CkJsonObject_put_EmitCompact $jResp 0
puts [CkJsonObject_emit $jResp]

# The send succeeded if the response status code = 201.
if {[CkHttpResponse_get_StatusCode $resp] != 201} then {
    puts "Failed"
    delete_CkHttp $http
    delete_CkJsonObject $jsonToken
    delete_CkJsonObject $json
    delete_CkCrypt2 $crypt
    delete_CkHttpResponse $resp
    delete_CkJsonObject $jResp
    exit
}

# Sample JSON response:
# (Sample code for parsing the JSON response is shown below)

# {
#   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('user%40example.com')/events/$entity",
#   "@odata.etag": "W/\"5+vF7TKKdE6bGCRqXyl2PQAEaGQgcw==\"",
#   "id": "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5_vF7TKKdE6bGCRqXyl2PQAAAgENAAAA5_vF7TKKdE6bGCRqXyl2PQAEaDkEcAAAAA==",
#   "createdDateTime": "2021-04-18T23:38:52.1979259Z",
#   "lastModifiedDateTime": "2021-04-18T23:38:53.3747647Z",
#   "changeKey": "5+vF7TKKdE6bGCRqXyl2PQAEaGQgcw==",
#   "categories": [
#   ],
#   "transactionId": "1d12b565-3ca3-4ed8-b3f9-e8a14ac3ac17",
#   "originalStartTimeZone": "Pacific Standard Time",
#   "originalEndTimeZone": "Pacific Standard Time",
#   "iCalUId": "040000008200E00074C5B7101A82E00800000000EF0328FDAB34D7010000000000000000100000004478DD5948382543AFD1B52C25E88C02",
#   "reminderMinutesBeforeStart": 15,
#   "isReminderOn": true,
#   "hasAttachments": false,
#   "subject": "Let's go for lunch",
#   "bodyPreview": "Does noon work for you?",
#   "importance": "normal",
#   "sensitivity": "normal",
#   "isAllDay": false,
#   "isCancelled": false,
#   "isOrganizer": true,
#   "responseRequested": true,
#   "seriesMasterId": null,
#   "showAs": "busy",
#   "type": "singleInstance",
#   "webLink": "https://outlook.live.com/owa/?itemid=AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5%2BvF7TKKdE6bGCRqXyl2PQAAAgENAAAA5%2BvF7TKKdE6bGCRqXyl2PQAEaDkEcAAAAA%3D%3D&exvsurl=1&path=/calendar/item",
#   "onlineMeetingUrl": null,
#   "isOnlineMeeting": false,
#   "onlineMeetingProvider": "unknown",
#   "allowNewTimeProposals": true,
#   "isDraft": false,
#   "hideAttendees": false,
#   "recurrence": null,
#   "onlineMeeting": null,
#   "responseStatus": {
#     "response": "organizer",
#     "time": "0001-01-01T00:00:00Z"
#   },
#   "body": {
#     "contentType": "html",
#     "content": "<html>\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\r\n<meta content=\"text/html; charset=us-ascii\">\r\n</head>\r\n<body>\r\nDoes noon work for you?\r\n</body>\r\n</html>\r\n"
#   },
#   "start": {
#     "dateTime": "2021-05-15T12:00:00.0000000",
#     "timeZone": "Pacific Standard Time"
#   },
#   "end": {
#     "dateTime": "2021-05-15T14:00:00.0000000",
#     "timeZone": "Pacific Standard Time"
#   },
#   "location": {
#     "displayName": "Harry's Bar",
#     "locationType": "default",
#     "uniqueId": "Harry's Bar",
#     "uniqueIdType": "private"
#   },
#   "locations": [
#     {
#       "displayName": "Harry's Bar",
#       "locationType": "default",
#       "uniqueId": "Harry's Bar",
#       "uniqueIdType": "private"
#     }
#   ],
#   "attendees": [
#     {
#       "type": "required",
#       "status": {
#         "response": "none",
#         "time": "0001-01-01T00:00:00Z"
#       },
#       "emailAddress": {
#         "name": "Samantha Booth",
#         "address": "samanthab@contoso.onmicrosoft.com"
#       }
#     }
#   ],
#   "organizer": {
#     "emailAddress": {
#       "name": "John Doe",
#       "address": "outlook_3A33FCEB9B74CC15@outlook.com"
#     }
#   }
# }

# Sample code for parsing the JSON response...
# Use the following online tool to generate parsing code from sample JSON:
# Generate Parsing Code from JSON

set odata_context [CkJsonObject_stringOf $jResp "\"@odata.context\""]
set odata_etag [CkJsonObject_stringOf $jResp "\"@odata.etag\""]
set id [CkJsonObject_stringOf $jResp "id"]
set createdDateTime [CkJsonObject_stringOf $jResp "createdDateTime"]
set lastModifiedDateTime [CkJsonObject_stringOf $jResp "lastModifiedDateTime"]
set changeKey [CkJsonObject_stringOf $jResp "changeKey"]
set transactionId [CkJsonObject_stringOf $jResp "transactionId"]
set originalStartTimeZone [CkJsonObject_stringOf $jResp "originalStartTimeZone"]
set originalEndTimeZone [CkJsonObject_stringOf $jResp "originalEndTimeZone"]
set iCalUId [CkJsonObject_stringOf $jResp "iCalUId"]
set reminderMinutesBeforeStart [CkJsonObject_IntOf $jResp "reminderMinutesBeforeStart"]
set isReminderOn [CkJsonObject_BoolOf $jResp "isReminderOn"]
set hasAttachments [CkJsonObject_BoolOf $jResp "hasAttachments"]
set subject [CkJsonObject_stringOf $jResp "subject"]
set bodyPreview [CkJsonObject_stringOf $jResp "bodyPreview"]
set importance [CkJsonObject_stringOf $jResp "importance"]
set sensitivity [CkJsonObject_stringOf $jResp "sensitivity"]
set isAllDay [CkJsonObject_BoolOf $jResp "isAllDay"]
set isCancelled [CkJsonObject_BoolOf $jResp "isCancelled"]
set isOrganizer [CkJsonObject_BoolOf $jResp "isOrganizer"]
set responseRequested [CkJsonObject_BoolOf $jResp "responseRequested"]
set seriesMasterId [CkJsonObject_stringOf $jResp "seriesMasterId"]
set showAs [CkJsonObject_stringOf $jResp "showAs"]
set v_type [CkJsonObject_stringOf $jResp "type"]
set webLink [CkJsonObject_stringOf $jResp "webLink"]
set onlineMeetingUrl [CkJsonObject_stringOf $jResp "onlineMeetingUrl"]
set isOnlineMeeting [CkJsonObject_BoolOf $jResp "isOnlineMeeting"]
set onlineMeetingProvider [CkJsonObject_stringOf $jResp "onlineMeetingProvider"]
set allowNewTimeProposals [CkJsonObject_BoolOf $jResp "allowNewTimeProposals"]
set isDraft [CkJsonObject_BoolOf $jResp "isDraft"]
set hideAttendees [CkJsonObject_BoolOf $jResp "hideAttendees"]
set recurrence [CkJsonObject_stringOf $jResp "recurrence"]
set onlineMeeting [CkJsonObject_stringOf $jResp "onlineMeeting"]
set responseStatusResponse [CkJsonObject_stringOf $jResp "responseStatus.response"]
set responseStatusTime [CkJsonObject_stringOf $jResp "responseStatus.time"]
set bodyContentType [CkJsonObject_stringOf $jResp "body.contentType"]
set bodyContent [CkJsonObject_stringOf $jResp "body.content"]
set startDateTime [CkJsonObject_stringOf $jResp "start.dateTime"]
set startTimeZone [CkJsonObject_stringOf $jResp "start.timeZone"]
set endDateTime [CkJsonObject_stringOf $jResp "end.dateTime"]
set endTimeZone [CkJsonObject_stringOf $jResp "end.timeZone"]
set locationDisplayName [CkJsonObject_stringOf $jResp "location.displayName"]
set locationLocationType [CkJsonObject_stringOf $jResp "location.locationType"]
set locationUniqueId [CkJsonObject_stringOf $jResp "location.uniqueId"]
set locationUniqueIdType [CkJsonObject_stringOf $jResp "location.uniqueIdType"]
set organizerEmailAddressName [CkJsonObject_stringOf $jResp "organizer.emailAddress.name"]
set organizerEmailAddressAddress [CkJsonObject_stringOf $jResp "organizer.emailAddress.address"]
set i 0
set count_i [CkJsonObject_SizeOfArray $jResp "categories"]
while {$i < $count_i} {
    CkJsonObject_put_I $jResp $i
    set i [expr $i + 1]
}
set i 0
set count_i [CkJsonObject_SizeOfArray $jResp "locations"]
while {$i < $count_i} {
    CkJsonObject_put_I $jResp $i
    set displayName [CkJsonObject_stringOf $jResp "locations[i].displayName"]
    set locationType [CkJsonObject_stringOf $jResp "locations[i].locationType"]
    set uniqueId [CkJsonObject_stringOf $jResp "locations[i].uniqueId"]
    set uniqueIdType [CkJsonObject_stringOf $jResp "locations[i].uniqueIdType"]
    set i [expr $i + 1]
}
set i 0
set count_i [CkJsonObject_SizeOfArray $jResp "attendees"]
while {$i < $count_i} {
    CkJsonObject_put_I $jResp $i
    set v_type [CkJsonObject_stringOf $jResp "attendees[i].type"]
    set statusResponse [CkJsonObject_stringOf $jResp "attendees[i].status.response"]
    set statusTime [CkJsonObject_stringOf $jResp "attendees[i].status.time"]
    set emailAddressName [CkJsonObject_stringOf $jResp "attendees[i].emailAddress.name"]
    set emailAddressAddress [CkJsonObject_stringOf $jResp "attendees[i].emailAddress.address"]
    set i [expr $i + 1]
}

puts "Event created."

delete_CkHttp $http
delete_CkJsonObject $jsonToken
delete_CkJsonObject $json
delete_CkCrypt2 $crypt
delete_CkHttpResponse $resp
delete_CkJsonObject $jResp