Swift
Swift
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 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 as shown here:
// Get Microsoft Graph OAuth2 Access Token with Calendars.ReadWrite scope.
let jsonToken = CkoJsonObject()!
success = jsonToken.loadFile(path: "qa_data/tokens/msGraphCalendar.json")
if success == false {
print("\(jsonToken.lastErrorText!)")
return
}
http.authToken = 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"
// }
// ]
// }
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 mid month work for you?")
json.updateString(jsonPath: "start.dateTime", value: "2019-11-15T12:00:00")
json.updateString(jsonPath: "start.timeZone", value: "Pacific Standard Time")
json.updateString(jsonPath: "end.dateTime", value: "2019-11-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: "adelev@contoso.onmicrosoft.com")
json.updateString(jsonPath: "attendees[0].emailAddress.name", value: "Adele Vance")
json.updateString(jsonPath: "attendees[0].type", value: "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.setUrlVar(name: "id", value: "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5_vF7TKKdE6bGCRqXyl2PQAAAgEGAAAA5_vF7TKKdE6bGCRqXyl2PQAClEpRTgAAAA==")
let resp = CkoHttpResponse()!
success = http.httpJson(verb: "POST", url: "https://graph.microsoft.com/v1.0/me/calendars/{$id}/events", json: json, contentType: "application/json", response: resp)
if success == false {
print("\(http.lastErrorText!)")
return
}
json.load(json: resp.bodyStr)
json.emitCompact = false
if resp.statusCode.intValue != 201 {
print("\(json.emit()!)")
print("Failed, response status code = \(resp.statusCode.intValue)")
return
}
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
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 odataContext: String? = json.string(of: "\"@odata.context\"")
var odataEtag: String? = json.string(of: "\"@odata.etag\"")
var id: String? = json.string(of: "id")
var createdDateTime: String? = json.string(of: "createdDateTime")
var lastModifiedDateTime: String? = json.string(of: "lastModifiedDateTime")
var changeKey: String? = json.string(of: "changeKey")
var originalStartTimeZone: String? = json.string(of: "originalStartTimeZone")
var originalEndTimeZone: String? = json.string(of: "originalEndTimeZone")
var iCalUId: String? = json.string(of: "iCalUId")
var reminderMinutesBeforeStart: String? = json.int(of: "reminderMinutesBeforeStart").intValue
var isReminderOn: String? = json.bool(of: "isReminderOn")
var hasAttachments: String? = json.bool(of: "hasAttachments")
var subject: String? = json.string(of: "subject")
var bodyPreview: String? = json.string(of: "bodyPreview")
var importance: String? = json.string(of: "importance")
var sensitivity: String? = json.string(of: "sensitivity")
var isAllDay: String? = json.bool(of: "isAllDay")
var isCancelled: String? = json.bool(of: "isCancelled")
var isOrganizer: String? = json.bool(of: "isOrganizer")
var responseRequested: String? = json.bool(of: "responseRequested")
var seriesMasterId: String? = json.string(of: "seriesMasterId")
var showAs: String? = json.string(of: "showAs")
var type: String? = json.string(of: "type")
var webLink: String? = json.string(of: "webLink")
var onlineMeetingUrl: String? = json.string(of: "onlineMeetingUrl")
var recurrence: String? = json.string(of: "recurrence")
var responseStatusResponse: String? = json.string(of: "responseStatus.response")
var responseStatusTime: String? = json.string(of: "responseStatus.time")
var bodyContentType: String? = json.string(of: "body.contentType")
var bodyContent: String? = json.string(of: "body.content")
var startDateTime: String? = json.string(of: "start.dateTime")
var startTimeZone: String? = json.string(of: "start.timeZone")
var endDateTime: String? = json.string(of: "end.dateTime")
var endTimeZone: String? = json.string(of: "end.timeZone")
var locationDisplayName: String? = json.string(of: "location.displayName")
var locationLocationType: String? = json.string(of: "location.locationType")
var locationUniqueId: String? = json.string(of: "location.uniqueId")
var locationUniqueIdType: String? = json.string(of: "location.uniqueIdType")
var organizerEmailAddressName: String? = json.string(of: "organizer.emailAddress.name")
var organizerEmailAddressAddress: String? = json.string(of: "organizer.emailAddress.address")
var i: Int = 0
var count_i: Int = json.size(ofArray: "categories").intValue
while i < count_i {
json.i = i
// ...
i = i + 1
}
i = 0
count_i = json.size(ofArray: "locations").intValue
while i < count_i {
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
}
i = 0
count_i = json.size(ofArray: "attendees").intValue
while i < count_i {
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
}
print("Success.")
}