Sample code for 30+ languages & platforms
Zig

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 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 here: Get Outlook Calendar OAuth2 Access Token (Azure AD v2.0 Endpoint).

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

    http.setAuthToken(try json_token.stringOf(alloc, "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.
    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 noon work for you?") catch {};
    json.updateString("start.dateTime", "2021-05-15T12:00:00") catch {};
    json.updateString("start.timeZone", "Pacific Standard Time") catch {};
    json.updateString("end.dateTime", "2021-05-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", "samanthab@contoso.onmicrosoft.com") catch {};
    json.updateString("attendees[0].emailAddress.name", "Samantha Booth") catch {};
    json.updateString("attendees[0].type", "required") catch {};
    json.updateBool("allowNewTimeProposals", true) catch {};

    // Generate a UUID.
    const crypt = try chilkat.Crypt2.init();
    defer crypt.deinit();
    json.updateString("transactionId", try crypt.generateUuid(alloc)) catch {};

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

    // Send the HTTP POST
    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", "https://graph.microsoft.com/v1.0/me/events", json, "application/json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Response status code = {d}\n", .{resp.getStatusCode()});

    const j_resp = try chilkat.JsonObject.init();
    defer j_resp.deinit();
    j_resp.load(try resp.getBodyStr(alloc)) catch {};
    j_resp.setEmitCompact(false);
    std.debug.print("{s}\n", .{try j_resp.emit(alloc)});

    // The send succeeded if the response status code = 201.
    if (resp.getStatusCode() != 201) {
        std.debug.print("Failed\n", .{});
        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

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

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

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

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