Sample code for 30+ languages & platforms
Lua

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 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 here: Get Outlook Calendar OAuth2 Access Token (Azure AD v2.0 Endpoint).

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

http.auth_token = jsonToken:string_of("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.
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 noon work for you?")
json:update_string("start.dateTime", "2021-05-15T12:00:00")
json:update_string("start.timeZone", "Pacific Standard Time")
json:update_string("end.dateTime", "2021-05-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", "samanthab@contoso.onmicrosoft.com")
json:update_string("attendees[0].emailAddress.name", "Samantha Booth")
json:update_string("attendees[0].type", "required")
json:update_bool("allowNewTimeProposals", true)

-- Generate a UUID.
local crypt = chilkat.Crypt2.new()
json:update_string("transactionId", crypt:generate_uuid())

-- Add the "Prefer" request header.
http:set_request_header("Prefer", "outlook.timezone=\"Pacific Standard Time\"")

-- Send the HTTP POST
local resp = chilkat.HttpResponse.new()
success = http:http_json("POST", "https://graph.microsoft.com/v1.0/me/events", json, "application/json", resp)
if not success then
    print(http.last_error_text)
    return
end

print("Response status code = " .. resp.status_code)

local jResp = chilkat.JsonObject.new()
jResp:load(resp.body_str)
jResp.emit_compact = false
print(jResp:emit())

-- The send succeeded if the response status code = 201.
if resp.status_code ~= 201 then
    print("Failed")
    return
end

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

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

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

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

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

print("Event created.")