Sample code for 30+ languages & platforms
C

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

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttp http;
    HCkJsonObject jsonToken;
    HCkJsonObject json;
    HCkCrypt2 crypt;
    HCkHttpResponse resp;
    HCkJsonObject jResp;
    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 *odata_context;
    const char *odata_etag;
    const char *id;
    const char *createdDateTime;
    const char *lastModifiedDateTime;
    const char *changeKey;
    const char *transactionId;
    const char *originalStartTimeZone;
    const char *originalEndTimeZone;
    const char *iCalUId;
    int reminderMinutesBeforeStart;
    BOOL isReminderOn;
    BOOL hasAttachments;
    const char *subject;
    const char *bodyPreview;
    const char *importance;
    const char *sensitivity;
    BOOL isAllDay;
    BOOL isCancelled;
    BOOL isOrganizer;
    BOOL responseRequested;
    const char *seriesMasterId;
    const char *showAs;
    const char *v_type;
    const char *webLink;
    const char *onlineMeetingUrl;
    BOOL isOnlineMeeting;
    const char *onlineMeetingProvider;
    BOOL allowNewTimeProposals;
    BOOL isDraft;
    BOOL hideAttendees;
    const char *recurrence;
    const char *onlineMeeting;
    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 here: Get Outlook Calendar OAuth2 Access Token (Azure AD v2.0 Endpoint).

    jsonToken = CkJsonObject_Create();
    success = CkJsonObject_LoadFile(jsonToken,"qa_data/tokens/outlookCalendar.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"));

    // 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.
    json = CkJsonObject_Create();
    CkJsonObject_UpdateString(json,"subject","Let's go for lunch");
    CkJsonObject_UpdateString(json,"body.contentType","HTML");
    CkJsonObject_UpdateString(json,"body.content","Does noon work for you?");
    CkJsonObject_UpdateString(json,"start.dateTime","2021-05-15T12:00:00");
    CkJsonObject_UpdateString(json,"start.timeZone","Pacific Standard Time");
    CkJsonObject_UpdateString(json,"end.dateTime","2021-05-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","samanthab@contoso.onmicrosoft.com");
    CkJsonObject_UpdateString(json,"attendees[0].emailAddress.name","Samantha Booth");
    CkJsonObject_UpdateString(json,"attendees[0].type","required");
    CkJsonObject_UpdateBool(json,"allowNewTimeProposals",TRUE);

    // Generate a UUID.
    crypt = CkCrypt2_Create();
    CkJsonObject_UpdateString(json,"transactionId",CkCrypt2_generateUuid(crypt));

    // Add the "Prefer" request header.
    CkHttp_SetRequestHeader(http,"Prefer","outlook.timezone=\"Pacific Standard Time\"");

    // Send the HTTP POST
    resp = CkHttpResponse_Create();
    success = CkHttp_HttpJson(http,"POST","https://graph.microsoft.com/v1.0/me/events",json,"application/json",resp);
    if (success == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(jsonToken);
        CkJsonObject_Dispose(json);
        CkCrypt2_Dispose(crypt);
        CkHttpResponse_Dispose(resp);
        return;
    }

    printf("Response status code = %d\n",CkHttpResponse_getStatusCode(resp));

    jResp = CkJsonObject_Create();
    CkJsonObject_Load(jResp,CkHttpResponse_bodyStr(resp));
    CkJsonObject_putEmitCompact(jResp,FALSE);
    printf("%s\n",CkJsonObject_emit(jResp));

    // The send succeeded if the response status code = 201.
    if (CkHttpResponse_getStatusCode(resp) != 201) {
        printf("Failed\n");
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(jsonToken);
        CkJsonObject_Dispose(json);
        CkCrypt2_Dispose(crypt);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(jResp);
        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

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
    // See this example explaining how this memory should be used: const char * functions.

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

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

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

    printf("Event created.\n");


    CkHttp_Dispose(http);
    CkJsonObject_Dispose(jsonToken);
    CkJsonObject_Dispose(json);
    CkCrypt2_Dispose(crypt);
    CkHttpResponse_Dispose(resp);
    CkJsonObject_Dispose(jResp);

    }