Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

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 Lua Downloads

Lua
local chilkat = require("chilkat")

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

local http = chilkat.Http.new()

-- Use your previously obtained access token as shown here:
--    Get Microsoft Graph OAuth2 Access Token with Calendars.ReadWrite scope.

local jsonToken = chilkat.JsonObject.new()
local success = jsonToken:load_file("qa_data/tokens/msGraphCalendar.json")
if not success then
    print(jsonToken.last_error_text)
    return
end

http.auth_token = jsonToken:string_of("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"
--     }
--   ]
-- }

local json = chilkat.JsonObject.new()
json:update_string("subject", "Let's go for lunch")
json:update_string("body.contentType", "HTML")
json:update_string("body.content", "Does mid month work for you?")
json:update_string("start.dateTime", "2019-11-15T12:00:00")
json:update_string("start.timeZone", "Pacific Standard Time")
json:update_string("end.dateTime", "2019-11-15T14:00:00")
json:update_string("end.timeZone", "Pacific Standard Time")
json:update_string("location.displayName", "Harry's Bar")
json:update_string("attendees[0].emailAddress.address", "adelev@contoso.onmicrosoft.com")
json:update_string("attendees[0].emailAddress.name", "Adele Vance")
json:update_string("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
http:set_url_var("id", "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5_vF7TKKdE6bGCRqXyl2PQAAAgEGAAAA5_vF7TKKdE6bGCRqXyl2PQAClEpRTgAAAA==")
local resp = chilkat.HttpResponse.new()
success = http:http_json("POST", "https://graph.microsoft.com/v1.0/me/calendars/{$id}/events", json, "application/json", resp)
if not success then
    print(http.last_error_text)
    return
end

json:load(resp.body_str)
json.emit_compact = false

if resp.status_code ~= 201 then
    print(json:emit())
    print("Failed, response status code = " .. resp.status_code)
    return
end

print(json:emit())

-- 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

local displayName = ""
local locationType = ""
local uniqueId = ""
local uniqueIdType = ""
local statusResponse = ""
local statusTime = ""
local emailAddressName = ""
local emailAddressAddress = ""

local odataContext = json:string_of("\"@odata.context\"")
local odataEtag = json:string_of("\"@odata.etag\"")
local id = json:string_of("id")
local createdDateTime = json:string_of("createdDateTime")
local lastModifiedDateTime = json:string_of("lastModifiedDateTime")
local changeKey = json:string_of("changeKey")
local originalStartTimeZone = json:string_of("originalStartTimeZone")
local originalEndTimeZone = json:string_of("originalEndTimeZone")
local iCalUId = json:string_of("iCalUId")
local reminderMinutesBeforeStart = json:int_of("reminderMinutesBeforeStart")
local isReminderOn = json:bool_of("isReminderOn")
local hasAttachments = json:bool_of("hasAttachments")
local subject = json:string_of("subject")
local bodyPreview = json:string_of("bodyPreview")
local importance = json:string_of("importance")
local sensitivity = json:string_of("sensitivity")
local isAllDay = json:bool_of("isAllDay")
local isCancelled = json:bool_of("isCancelled")
local isOrganizer = json:bool_of("isOrganizer")
local responseRequested = json:bool_of("responseRequested")
local seriesMasterId = json:string_of("seriesMasterId")
local showAs = json:string_of("showAs")
local type_ = json:string_of("type")
local webLink = json:string_of("webLink")
local onlineMeetingUrl = json:string_of("onlineMeetingUrl")
local recurrence = json:string_of("recurrence")
local responseStatusResponse = json:string_of("responseStatus.response")
local responseStatusTime = json:string_of("responseStatus.time")
local bodyContentType = json:string_of("body.contentType")
local bodyContent = json:string_of("body.content")
local startDateTime = json:string_of("start.dateTime")
local startTimeZone = json:string_of("start.timeZone")
local endDateTime = json:string_of("end.dateTime")
local endTimeZone = json:string_of("end.timeZone")
local locationDisplayName = json:string_of("location.displayName")
local locationLocationType = json:string_of("location.locationType")
local locationUniqueId = json:string_of("location.uniqueId")
local locationUniqueIdType = json:string_of("location.uniqueIdType")
local organizerEmailAddressName = json:string_of("organizer.emailAddress.name")
local organizerEmailAddressAddress = json:string_of("organizer.emailAddress.address")
local i = 0
local count_i = json:size_of_array("categories")
while i < count_i do
    json.i = i
    -- ...
    i = i + 1
end

i = 0
count_i = json:size_of_array("locations")
while i < count_i do
    json.i = i
    displayName = json:string_of("locations[i].displayName")
    locationType = json:string_of("locations[i].locationType")
    uniqueId = json:string_of("locations[i].uniqueId")
    uniqueIdType = json:string_of("locations[i].uniqueIdType")
    i = i + 1
end

i = 0
count_i = json:size_of_array("attendees")
while i < count_i do
    json.i = i
    type_ = json:string_of("attendees[i].type")
    statusResponse = json:string_of("attendees[i].status.response")
    statusTime = json:string_of("attendees[i].status.time")
    emailAddressName = json:string_of("attendees[i].emailAddress.name")
    emailAddressAddress = json:string_of("attendees[i].emailAddress.address")
    i = i + 1
end

print("Success.")