Swift
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = CkoHttp()!
// Use your previously obtained access token here: Get Outlook Calendar OAuth2 Access Token (Azure AD v2.0 Endpoint).
let jsonToken = CkoJsonObject()!
success = jsonToken.loadFile(path: "qa_data/tokens/outlookCalendar.json")
if success == false {
print("\(jsonToken.lastErrorText!)")
return
}
http.authToken = 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.
let json = CkoJsonObject()!
json.updateString(jsonPath: "subject", value: "Let's go for lunch")
json.updateString(jsonPath: "body.contentType", value: "HTML")
json.updateString(jsonPath: "body.content", value: "Does noon work for you?")
json.updateString(jsonPath: "start.dateTime", value: "2021-05-15T12:00:00")
json.updateString(jsonPath: "start.timeZone", value: "Pacific Standard Time")
json.updateString(jsonPath: "end.dateTime", value: "2021-05-15T14:00:00")
json.updateString(jsonPath: "end.timeZone", value: "Pacific Standard Time")
json.updateString(jsonPath: "location.displayName", value: "Harry's Bar")
json.updateString(jsonPath: "attendees[0].emailAddress.address", value: "samanthab@contoso.onmicrosoft.com")
json.updateString(jsonPath: "attendees[0].emailAddress.name", value: "Samantha Booth")
json.updateString(jsonPath: "attendees[0].type", value: "required")
json.updateBool(jsonPath: "allowNewTimeProposals", value: true)
// Generate a UUID.
let crypt = CkoCrypt2()!
json.updateString(jsonPath: "transactionId", value: crypt.generateUuid())
// Add the "Prefer" request header.
http.setRequestHeader(name: "Prefer", value: "outlook.timezone=\"Pacific Standard Time\"")
// Send the HTTP POST
let resp = CkoHttpResponse()!
success = http.httpJson(verb: "POST", url: "https://graph.microsoft.com/v1.0/me/events", json: json, contentType: "application/json", response: resp)
if success == false {
print("\(http.lastErrorText!)")
return
}
print("Response status code = \(resp.statusCode.intValue)")
let jResp = CkoJsonObject()!
jResp.load(json: resp.bodyStr)
jResp.emitCompact = false
print("\(jResp.emit()!)")
// The send succeeded if the response status code = 201.
if resp.statusCode.intValue != 201 {
print("Failed")
return
}
// 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
var displayName: String?
var locationType: String?
var uniqueId: String?
var uniqueIdType: String?
var statusResponse: String?
var statusTime: String?
var emailAddressName: String?
var emailAddressAddress: String?
var odata_context: String? = jResp.string(of: "\"@odata.context\"")
var odata_etag: String? = jResp.string(of: "\"@odata.etag\"")
var id: String? = jResp.string(of: "id")
var createdDateTime: String? = jResp.string(of: "createdDateTime")
var lastModifiedDateTime: String? = jResp.string(of: "lastModifiedDateTime")
var changeKey: String? = jResp.string(of: "changeKey")
var transactionId: String? = jResp.string(of: "transactionId")
var originalStartTimeZone: String? = jResp.string(of: "originalStartTimeZone")
var originalEndTimeZone: String? = jResp.string(of: "originalEndTimeZone")
var iCalUId: String? = jResp.string(of: "iCalUId")
var reminderMinutesBeforeStart: Int = jResp.int(of: "reminderMinutesBeforeStart").intValue
var isReminderOn: Bool = jResp.bool(of: "isReminderOn")
var hasAttachments: Bool = jResp.bool(of: "hasAttachments")
var subject: String? = jResp.string(of: "subject")
var bodyPreview: String? = jResp.string(of: "bodyPreview")
var importance: String? = jResp.string(of: "importance")
var sensitivity: String? = jResp.string(of: "sensitivity")
var isAllDay: Bool = jResp.bool(of: "isAllDay")
var isCancelled: Bool = jResp.bool(of: "isCancelled")
var isOrganizer: Bool = jResp.bool(of: "isOrganizer")
var responseRequested: Bool = jResp.bool(of: "responseRequested")
var seriesMasterId: String? = jResp.string(of: "seriesMasterId")
var showAs: String? = jResp.string(of: "showAs")
var v_type: String? = jResp.string(of: "type")
var webLink: String? = jResp.string(of: "webLink")
var onlineMeetingUrl: String? = jResp.string(of: "onlineMeetingUrl")
var isOnlineMeeting: Bool = jResp.bool(of: "isOnlineMeeting")
var onlineMeetingProvider: String? = jResp.string(of: "onlineMeetingProvider")
var allowNewTimeProposals: Bool = jResp.bool(of: "allowNewTimeProposals")
var isDraft: Bool = jResp.bool(of: "isDraft")
var hideAttendees: Bool = jResp.bool(of: "hideAttendees")
var recurrence: String? = jResp.string(of: "recurrence")
var onlineMeeting: String? = jResp.string(of: "onlineMeeting")
var responseStatusResponse: String? = jResp.string(of: "responseStatus.response")
var responseStatusTime: String? = jResp.string(of: "responseStatus.time")
var bodyContentType: String? = jResp.string(of: "body.contentType")
var bodyContent: String? = jResp.string(of: "body.content")
var startDateTime: String? = jResp.string(of: "start.dateTime")
var startTimeZone: String? = jResp.string(of: "start.timeZone")
var endDateTime: String? = jResp.string(of: "end.dateTime")
var endTimeZone: String? = jResp.string(of: "end.timeZone")
var locationDisplayName: String? = jResp.string(of: "location.displayName")
var locationLocationType: String? = jResp.string(of: "location.locationType")
var locationUniqueId: String? = jResp.string(of: "location.uniqueId")
var locationUniqueIdType: String? = jResp.string(of: "location.uniqueIdType")
var organizerEmailAddressName: String? = jResp.string(of: "organizer.emailAddress.name")
var organizerEmailAddressAddress: String? = jResp.string(of: "organizer.emailAddress.address")
var i: Int = 0
var count_i: Int = jResp.size(ofArray: "categories").intValue
while i < count_i {
jResp.i = i
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "locations").intValue
while i < count_i {
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
}
i = 0
count_i = jResp.size(ofArray: "attendees").intValue
while i < count_i {
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
}
print("Event created.")
}