Sample code for 30+ languages & platforms
Tcl

MS Graph Calendar Create Event

See more Microsoft Calendar Examples

Creates a new event in a specified calendar.

For more details, see https://docs.microsoft.com/en-us/graph/api/calendar-post-events?view=graph-rest-1.0

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 as shown here:
#    Get Microsoft Graph OAuth2 Access Token with Calendars.ReadWrite scope.

set jsonToken [new_CkJsonObject]

set success [CkJsonObject_LoadFile $jsonToken "qa_data/tokens/msGraphCalendar.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"]

# Create a JSON body for the HTTP POST
# Use this online tool to generate the code from sample JSON: 
# Generate Code to Create JSON

# {
#   "subject": "Let's go for lunch",
#   "body": {
#     "contentType": "HTML",
#     "content": "Does mid month work for you?"
#   },
#   "start": {
#       "dateTime": "2019-11-15T12:00:00",
#       "timeZone": "Pacific Standard Time"
#   },
#   "end": {
#       "dateTime": "2019-11-15T14:00:00",
#       "timeZone": "Pacific Standard Time"
#   },
#   "location":{
#       "displayName":"Harry's Bar"
#   },
#   "attendees": [
#     {
#       "emailAddress": {
#         "address":"adelev@contoso.onmicrosoft.com",
#         "name": "Adele Vance"
#       },
#       "type": "required"
#     }
#   ]
# }

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 mid month work for you?"
CkJsonObject_UpdateString $json "start.dateTime" "2019-11-15T12:00:00"
CkJsonObject_UpdateString $json "start.timeZone" "Pacific Standard Time"
CkJsonObject_UpdateString $json "end.dateTime" "2019-11-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" "adelev@contoso.onmicrosoft.com"
CkJsonObject_UpdateString $json "attendees[0].emailAddress.name" "Adele Vance"
CkJsonObject_UpdateString $json "attendees[0].type" "required"

# POST the JSON to https://graph.microsoft.com/v1.0/me/calendars/{id}/events
# This is posting to a calendar in the default calendarGroup.

# Specify the calendar id
CkHttp_SetUrlVar $http "id" "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5_vF7TKKdE6bGCRqXyl2PQAAAgEGAAAA5_vF7TKKdE6bGCRqXyl2PQAClEpRTgAAAA=="
set resp [new_CkHttpResponse]

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

CkJsonObject_Load $json [CkHttpResponse_bodyStr $resp]
CkJsonObject_put_EmitCompact $json 0

if {[CkHttpResponse_get_StatusCode $resp] != 201} then {
    puts [CkJsonObject_emit $json]
    puts "Failed, response status code = [CkHttpResponse_get_StatusCode $resp]"
    delete_CkHttp $http
    delete_CkJsonObject $jsonToken
    delete_CkJsonObject $json
    delete_CkHttpResponse $resp
    exit
}

puts [CkJsonObject_emit $json]

# A sample response:
# (See code for parsing this response below..)

# {
#   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('admin%40chilkat.io')/calendars('AQMkADAwATM0MDAAMS ... AClEpRTgAAAA%3D%3D')/events/$entity",
#   "@odata.etag": "W/\"5+vF7TKKdE6bGCRqXyl2PQAClIgmmw==\"",
#   "id": "AQMkADAwAT ... ApRZ7JkAAAA=",
#   "createdDateTime": "2019-04-26T14:31:39.8791929Z",
#   "lastModifiedDateTime": "2019-04-26T14:31:41.2753537Z",
#   "changeKey": "5+vF7TKKdE6bGCRqXyl2PQAClIgmmw==",
#   "categories": [
#   ],
#   "originalStartTimeZone": "Pacific Standard Time",
#   "originalEndTimeZone": "Pacific Standard Time",
#   "iCalUId": "040000008200E00074C5B7101A82E00800000000F05DF1C23CFCD40100000000000000001000000009911D155F71EF42A230FEBFE5F7486A",
#   "reminderMinutesBeforeStart": 15,
#   "isReminderOn": true,
#   "hasAttachments": false,
#   "subject": "Let's go for lunch",
#   "bodyPreview": "Does mid month 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=AQMkADAwATM0MDAAMS1iNTcwL...pRZ7JkAAAA%3D&exvsurl=1&path=/calendar/item",
#   "onlineMeetingUrl": null,
#   "recurrence": 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 mid month work for you?\r\n</body>\r\n</html>\r\n"
#   },
#   "start": {
#     "dateTime": "2019-11-15T12:00:00.0000000",
#     "timeZone": "Pacific Standard Time"
#   },
#   "end": {
#     "dateTime": "2019-11-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": "Adele Vance",
#         "address": "adelev@contoso.onmicrosoft.com"
#       }
#     }
#   ],
#   "organizer": {
#     "emailAddress": {
#       "name": "Matt",
#       "address": "outlook_3A33FCEB9B74CC15@outlook.com"
#     }
#   }
# }
# 
# 

# Use this online tool to generate parsing code from sample JSON: 
# Generate Parsing Code from JSON

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

puts "Success."

delete_CkHttp $http
delete_CkJsonObject $jsonToken
delete_CkJsonObject $json
delete_CkHttpResponse $resp