Sample code for 30+ languages & platforms
C

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

C
#include <C_CkHttp.h>
#include <C_CkJsonObject.h>
#include <C_CkHttpResponse.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttp http;
    HCkJsonObject jsonToken;
    HCkJsonObject json;
    HCkHttpResponse resp;
    const char *displayName;
    const char *locationType;
    const char *uniqueId;
    const char *uniqueIdType;
    const char *statusResponse;
    const char *statusTime;
    const char *emailAddressName;
    const char *emailAddressAddress;
    const char *odataContext;
    const char *odataEtag;
    const char *id;
    const char *createdDateTime;
    const char *lastModifiedDateTime;
    const char *changeKey;
    const char *originalStartTimeZone;
    const char *originalEndTimeZone;
    const char *iCalUId;
    const char *reminderMinutesBeforeStart;
    const char *isReminderOn;
    const char *hasAttachments;
    const char *subject;
    const char *bodyPreview;
    const char *importance;
    const char *sensitivity;
    const char *isAllDay;
    const char *isCancelled;
    const char *isOrganizer;
    const char *responseRequested;
    const char *seriesMasterId;
    const char *showAs;
    const char *type;
    const char *webLink;
    const char *onlineMeetingUrl;
    const char *recurrence;
    const char *responseStatusResponse;
    const char *responseStatusTime;
    const char *bodyContentType;
    const char *bodyContent;
    const char *startDateTime;
    const char *startTimeZone;
    const char *endDateTime;
    const char *endTimeZone;
    const char *locationDisplayName;
    const char *locationLocationType;
    const char *locationUniqueId;
    const char *locationUniqueIdType;
    const char *organizerEmailAddressName;
    const char *organizerEmailAddressAddress;
    int i;
    int count_i;

    success = FALSE;

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

    http = CkHttp_Create();

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

    jsonToken = CkJsonObject_Create();
    success = CkJsonObject_LoadFile(jsonToken,"qa_data/tokens/msGraphCalendar.json");
    if (success == FALSE) {
        printf("%s\n",CkJsonObject_lastErrorText(jsonToken));
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(jsonToken);
        return;
    }

    CkHttp_putAuthToken(http,CkJsonObject_stringOf(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 = CkJsonObject_Create();
    CkJsonObject_UpdateString(json,"subject","Let's go for lunch");
    CkJsonObject_UpdateString(json,"body.contentType","HTML");
    CkJsonObject_UpdateString(json,"body.content","Does mid month work for you?");
    CkJsonObject_UpdateString(json,"start.dateTime","2019-11-15T12:00:00");
    CkJsonObject_UpdateString(json,"start.timeZone","Pacific Standard Time");
    CkJsonObject_UpdateString(json,"end.dateTime","2019-11-15T14:00:00");
    CkJsonObject_UpdateString(json,"end.timeZone","Pacific Standard Time");
    CkJsonObject_UpdateString(json,"location.displayName","Harry's Bar");
    CkJsonObject_UpdateString(json,"attendees[0].emailAddress.address","adelev@contoso.onmicrosoft.com");
    CkJsonObject_UpdateString(json,"attendees[0].emailAddress.name","Adele Vance");
    CkJsonObject_UpdateString(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_SetUrlVar(http,"id","AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5_vF7TKKdE6bGCRqXyl2PQAAAgEGAAAA5_vF7TKKdE6bGCRqXyl2PQAClEpRTgAAAA==");
    resp = CkHttpResponse_Create();
    success = CkHttp_HttpJson(http,"POST","https://graph.microsoft.com/v1.0/me/calendars/{$id}/events",json,"application/json",resp);
    if (success == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(jsonToken);
        CkJsonObject_Dispose(json);
        CkHttpResponse_Dispose(resp);
        return;
    }

    CkJsonObject_Load(json,CkHttpResponse_bodyStr(resp));
    CkJsonObject_putEmitCompact(json,FALSE);

    if (CkHttpResponse_getStatusCode(resp) != 201) {
        printf("%s\n",CkJsonObject_emit(json));
        printf("Failed, response status code = %d\n",CkHttpResponse_getStatusCode(resp));
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(jsonToken);
        CkJsonObject_Dispose(json);
        CkHttpResponse_Dispose(resp);
        return;
    }

    printf("%s\n",CkJsonObject_emit(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

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

    i = 0;
    count_i = CkJsonObject_SizeOfArray(json,"locations");
    while (i < count_i) {
        CkJsonObject_putI(json,i);
        displayName = CkJsonObject_stringOf(json,"locations[i].displayName");
        locationType = CkJsonObject_stringOf(json,"locations[i].locationType");
        uniqueId = CkJsonObject_stringOf(json,"locations[i].uniqueId");
        uniqueIdType = CkJsonObject_stringOf(json,"locations[i].uniqueIdType");
        i = i + 1;
    }

    i = 0;
    count_i = CkJsonObject_SizeOfArray(json,"attendees");
    while (i < count_i) {
        CkJsonObject_putI(json,i);
        type = CkJsonObject_stringOf(json,"attendees[i].type");
        statusResponse = CkJsonObject_stringOf(json,"attendees[i].status.response");
        statusTime = CkJsonObject_stringOf(json,"attendees[i].status.time");
        emailAddressName = CkJsonObject_stringOf(json,"attendees[i].emailAddress.name");
        emailAddressAddress = CkJsonObject_stringOf(json,"attendees[i].emailAddress.address");
        i = i + 1;
    }

    printf("Success.\n");


    CkHttp_Dispose(http);
    CkJsonObject_Dispose(jsonToken);
    CkJsonObject_Dispose(json);
    CkHttpResponse_Dispose(resp);

    }