Sample code for 30+ languages & platforms
PureBasic 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 PureBasic Downloads

PureBasic
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

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

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

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

    jsonToken.i = CkJsonObject::ckCreate()
    If jsonToken.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkJsonObject::ckLoadFile(jsonToken,"qa_data/tokens/msGraphCalendar.json")
    If success = 0
        Debug CkJsonObject::ckLastErrorText(jsonToken)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(jsonToken)
        ProcedureReturn
    EndIf

    CkHttp::setCkAuthToken(http, CkJsonObject::ckStringOf(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"
    ;      }
    ;    ]
    ;  }

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"subject","Let's go for lunch")
    CkJsonObject::ckUpdateString(json,"body.contentType","HTML")
    CkJsonObject::ckUpdateString(json,"body.content","Does mid month work for you?")
    CkJsonObject::ckUpdateString(json,"start.dateTime","2019-11-15T12:00:00")
    CkJsonObject::ckUpdateString(json,"start.timeZone","Pacific Standard Time")
    CkJsonObject::ckUpdateString(json,"end.dateTime","2019-11-15T14:00:00")
    CkJsonObject::ckUpdateString(json,"end.timeZone","Pacific Standard Time")
    CkJsonObject::ckUpdateString(json,"location.displayName","Harry's Bar")
    CkJsonObject::ckUpdateString(json,"attendees[0].emailAddress.address","adelev@contoso.onmicrosoft.com")
    CkJsonObject::ckUpdateString(json,"attendees[0].emailAddress.name","Adele Vance")
    CkJsonObject::ckUpdateString(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::ckSetUrlVar(http,"id","AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5_vF7TKKdE6bGCRqXyl2PQAAAgEGAAAA5_vF7TKKdE6bGCRqXyl2PQAClEpRTgAAAA==")
    resp.i = CkHttpResponse::ckCreate()
    If resp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckHttpJson(http,"POST","https://graph.microsoft.com/v1.0/me/calendars/{$id}/events",json,"application/json",resp)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(jsonToken)
        CkJsonObject::ckDispose(json)
        CkHttpResponse::ckDispose(resp)
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoad(json,CkHttpResponse::ckBodyStr(resp))
    CkJsonObject::setCkEmitCompact(json, 0)

    If CkHttpResponse::ckStatusCode(resp) <> 201
        Debug CkJsonObject::ckEmit(json)
        Debug "Failed, response status code = " + Str(CkHttpResponse::ckStatusCode(resp))
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(jsonToken)
        CkJsonObject::ckDispose(json)
        CkHttpResponse::ckDispose(resp)
        ProcedureReturn
    EndIf

    Debug CkJsonObject::ckEmit(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

    displayName.s
    locationType.s
    uniqueId.s
    uniqueIdType.s
    statusResponse.s
    statusTime.s
    emailAddressName.s
    emailAddressAddress.s

    odataContext.s = CkJsonObject::ckStringOf(json,Chr(34) + "@odata.context" + Chr(34))
    odataEtag.s = CkJsonObject::ckStringOf(json,Chr(34) + "@odata.etag" + Chr(34))
    id.s = CkJsonObject::ckStringOf(json,"id")
    createdDateTime.s = CkJsonObject::ckStringOf(json,"createdDateTime")
    lastModifiedDateTime.s = CkJsonObject::ckStringOf(json,"lastModifiedDateTime")
    changeKey.s = CkJsonObject::ckStringOf(json,"changeKey")
    originalStartTimeZone.s = CkJsonObject::ckStringOf(json,"originalStartTimeZone")
    originalEndTimeZone.s = CkJsonObject::ckStringOf(json,"originalEndTimeZone")
    iCalUId.s = CkJsonObject::ckStringOf(json,"iCalUId")
    reminderMinutesBeforeStart.s = CkJsonObject::ckIntOf(json,"reminderMinutesBeforeStart")
    isReminderOn.s = CkJsonObject::ckBoolOf(json,"isReminderOn")
    hasAttachments.s = CkJsonObject::ckBoolOf(json,"hasAttachments")
    subject.s = CkJsonObject::ckStringOf(json,"subject")
    bodyPreview.s = CkJsonObject::ckStringOf(json,"bodyPreview")
    importance.s = CkJsonObject::ckStringOf(json,"importance")
    sensitivity.s = CkJsonObject::ckStringOf(json,"sensitivity")
    isAllDay.s = CkJsonObject::ckBoolOf(json,"isAllDay")
    isCancelled.s = CkJsonObject::ckBoolOf(json,"isCancelled")
    isOrganizer.s = CkJsonObject::ckBoolOf(json,"isOrganizer")
    responseRequested.s = CkJsonObject::ckBoolOf(json,"responseRequested")
    seriesMasterId.s = CkJsonObject::ckStringOf(json,"seriesMasterId")
    showAs.s = CkJsonObject::ckStringOf(json,"showAs")
    type.s = CkJsonObject::ckStringOf(json,"type")
    webLink.s = CkJsonObject::ckStringOf(json,"webLink")
    onlineMeetingUrl.s = CkJsonObject::ckStringOf(json,"onlineMeetingUrl")
    recurrence.s = CkJsonObject::ckStringOf(json,"recurrence")
    responseStatusResponse.s = CkJsonObject::ckStringOf(json,"responseStatus.response")
    responseStatusTime.s = CkJsonObject::ckStringOf(json,"responseStatus.time")
    bodyContentType.s = CkJsonObject::ckStringOf(json,"body.contentType")
    bodyContent.s = CkJsonObject::ckStringOf(json,"body.content")
    startDateTime.s = CkJsonObject::ckStringOf(json,"start.dateTime")
    startTimeZone.s = CkJsonObject::ckStringOf(json,"start.timeZone")
    endDateTime.s = CkJsonObject::ckStringOf(json,"end.dateTime")
    endTimeZone.s = CkJsonObject::ckStringOf(json,"end.timeZone")
    locationDisplayName.s = CkJsonObject::ckStringOf(json,"location.displayName")
    locationLocationType.s = CkJsonObject::ckStringOf(json,"location.locationType")
    locationUniqueId.s = CkJsonObject::ckStringOf(json,"location.uniqueId")
    locationUniqueIdType.s = CkJsonObject::ckStringOf(json,"location.uniqueIdType")
    organizerEmailAddressName.s = CkJsonObject::ckStringOf(json,"organizer.emailAddress.name")
    organizerEmailAddressAddress.s = CkJsonObject::ckStringOf(json,"organizer.emailAddress.address")
    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(json,"categories")
    While i < count_i
        CkJsonObject::setCkI(json, i)
        ;  ...
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(json,"locations")
    While i < count_i
        CkJsonObject::setCkI(json, i)
        displayName = CkJsonObject::ckStringOf(json,"locations[i].displayName")
        locationType = CkJsonObject::ckStringOf(json,"locations[i].locationType")
        uniqueId = CkJsonObject::ckStringOf(json,"locations[i].uniqueId")
        uniqueIdType = CkJsonObject::ckStringOf(json,"locations[i].uniqueIdType")
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(json,"attendees")
    While i < count_i
        CkJsonObject::setCkI(json, i)
        type = CkJsonObject::ckStringOf(json,"attendees[i].type")
        statusResponse = CkJsonObject::ckStringOf(json,"attendees[i].status.response")
        statusTime = CkJsonObject::ckStringOf(json,"attendees[i].status.time")
        emailAddressName = CkJsonObject::ckStringOf(json,"attendees[i].emailAddress.name")
        emailAddressAddress = CkJsonObject::ckStringOf(json,"attendees[i].emailAddress.address")
        i = i + 1
    Wend

    Debug "Success."


    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(jsonToken)
    CkJsonObject::ckDispose(json)
    CkHttpResponse::ckDispose(resp)


    ProcedureReturn
EndProcedure