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

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    const http = try chilkat.Http.init();
    defer http.deinit();

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

    const json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    json_token.loadFile("qa_data/tokens/msGraphCalendar.json") catch {
        std.debug.print("{s}\n", .{try json_token.getLastErrorText(alloc)});
        return;
    };

    http.setAuthToken(try json_token.stringOf(alloc, "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"
    //     }
    //   ]
    // }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("subject", "Let's go for lunch") catch {};
    json.updateString("body.contentType", "HTML") catch {};
    json.updateString("body.content", "Does mid month work for you?") catch {};
    json.updateString("start.dateTime", "2019-11-15T12:00:00") catch {};
    json.updateString("start.timeZone", "Pacific Standard Time") catch {};
    json.updateString("end.dateTime", "2019-11-15T14:00:00") catch {};
    json.updateString("end.timeZone", "Pacific Standard Time") catch {};
    json.updateString("location.displayName", "Harry's Bar") catch {};
    json.updateString("attendees[0].emailAddress.address", "adelev@contoso.onmicrosoft.com") catch {};
    json.updateString("attendees[0].emailAddress.name", "Adele Vance") catch {};
    json.updateString("attendees[0].type", "required") catch {};

    // 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("id", "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5_vF7TKKdE6bGCRqXyl2PQAAAgEGAAAA5_vF7TKKdE6bGCRqXyl2PQAClEpRTgAAAA==") catch {};
    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", "https://graph.microsoft.com/v1.0/me/calendars/{$id}/events", json, "application/json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    json.load(try resp.getBodyStr(alloc)) catch {};
    json.setEmitCompact(false);

    if (resp.getStatusCode() != 201) {
        std.debug.print("{s}\n", .{try json.emit(alloc)});
        std.debug.print("Failed, response status code = {d}\n", .{resp.getStatusCode()});
        return;
    }

    std.debug.print("{s}\n", .{try json.emit(alloc)});

    // 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 display_name: [:0]const u8 = "";
    var location_type: [:0]const u8 = "";
    var unique_id: [:0]const u8 = "";
    var unique_id_type: [:0]const u8 = "";
    var status_response: [:0]const u8 = "";
    var status_time: [:0]const u8 = "";
    var email_address_name: [:0]const u8 = "";
    var email_address_address: [:0]const u8 = "";

    _ = try json.stringOf(alloc, "\"@odata.context\"");
    _ = try json.stringOf(alloc, "\"@odata.etag\"");
    _ = try json.stringOf(alloc, "id");
    _ = try json.stringOf(alloc, "createdDateTime");
    _ = try json.stringOf(alloc, "lastModifiedDateTime");
    _ = try json.stringOf(alloc, "changeKey");
    _ = try json.stringOf(alloc, "originalStartTimeZone");
    _ = try json.stringOf(alloc, "originalEndTimeZone");
    _ = try json.stringOf(alloc, "iCalUId");
    _ = json.intOf("reminderMinutesBeforeStart");
    _ = json.boolOf("isReminderOn");
    _ = json.boolOf("hasAttachments");
    _ = try json.stringOf(alloc, "subject");
    _ = try json.stringOf(alloc, "bodyPreview");
    _ = try json.stringOf(alloc, "importance");
    _ = try json.stringOf(alloc, "sensitivity");
    _ = json.boolOf("isAllDay");
    _ = json.boolOf("isCancelled");
    _ = json.boolOf("isOrganizer");
    _ = json.boolOf("responseRequested");
    _ = try json.stringOf(alloc, "seriesMasterId");
    _ = try json.stringOf(alloc, "showAs");
    var type_: [:0]const u8 = try json.stringOf(alloc, "type");
    _ = try json.stringOf(alloc, "webLink");
    _ = try json.stringOf(alloc, "onlineMeetingUrl");
    _ = try json.stringOf(alloc, "recurrence");
    _ = try json.stringOf(alloc, "responseStatus.response");
    _ = try json.stringOf(alloc, "responseStatus.time");
    _ = try json.stringOf(alloc, "body.contentType");
    _ = try json.stringOf(alloc, "body.content");
    _ = try json.stringOf(alloc, "start.dateTime");
    _ = try json.stringOf(alloc, "start.timeZone");
    _ = try json.stringOf(alloc, "end.dateTime");
    _ = try json.stringOf(alloc, "end.timeZone");
    _ = try json.stringOf(alloc, "location.displayName");
    _ = try json.stringOf(alloc, "location.locationType");
    _ = try json.stringOf(alloc, "location.uniqueId");
    _ = try json.stringOf(alloc, "location.uniqueIdType");
    _ = try json.stringOf(alloc, "organizer.emailAddress.name");
    _ = try json.stringOf(alloc, "organizer.emailAddress.address");
    var i: i32 = 0;
    var count_i: i32 = json.sizeOfArray("categories");
    while (i < count_i) {
        json.setI(i);
        // ...
        i = i + 1;
    }

    i = 0;
    count_i = json.sizeOfArray("locations");
    while (i < count_i) {
        json.setI(i);
        display_name = try json.stringOf(alloc, "locations[i].displayName");
        location_type = try json.stringOf(alloc, "locations[i].locationType");
        unique_id = try json.stringOf(alloc, "locations[i].uniqueId");
        unique_id_type = try json.stringOf(alloc, "locations[i].uniqueIdType");
        i = i + 1;
    }

    i = 0;
    count_i = json.sizeOfArray("attendees");
    while (i < count_i) {
        json.setI(i);
        type_ = try json.stringOf(alloc, "attendees[i].type");
        status_response = try json.stringOf(alloc, "attendees[i].status.response");
        status_time = try json.stringOf(alloc, "attendees[i].status.time");
        email_address_name = try json.stringOf(alloc, "attendees[i].emailAddress.name");
        email_address_address = try json.stringOf(alloc, "attendees[i].emailAddress.address");
        i = i + 1;
    }

    std.debug.print("Success.\n", .{});
}