Sample code for 30+ languages & platforms
Zig

MS Graph Calendar List Events

See more Microsoft Calendar Examples

Retrieve a list of events in a calendar. The list contains single instance meetings and series masters.

For more details, see https://docs.microsoft.com/en-us/graph/api/calendar-list-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"));

    // Specify the calendar id
    http.setUrlVar("id", "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5_vF7TKKdE6bGCRqXyl2PQAAAgEGAAAA5_vF7TKKdE6bGCRqXyl2PQAClEpRTgAAAA==") catch {};
    // Send a GET request to https://graph.microsoft.com/v1.0/me/calendars/{$id}/events
    const str_response = http.quickGetStr(alloc, "https://graph.microsoft.com/v1.0/me/calendars/{$id}/events") catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.load(str_response) catch {};
    json.setEmitCompact(false);

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

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

    // Sample output:
    // (See parsing code below..)

    // {
    //   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('admin%40chilkat.io')/calendars('AQMkADAwATM0MDAAMS1i ... AClEpRTgAAAA%3D%3D')/events",
    //   "value": [
    //     {
    //       "@odata.etag": "W/\"5+vF7TKKdE6bGCRqXyl2PQAClIgmmw==\"",
    //       "id": "AQMkADAwATM0MDAAM ... AApRZ7JkAAAA=",
    //       "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": "040000008200E ... A230FEBFE5F7486A",
    //       "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=AQMkADAwATM0MDAAMS1iNTcwLWI2NT ... gkal8pdj0AApRZ7JkAAAA%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-15T20:00:00.0000000",
    //         "timeZone": "UTC"
    //       },
    //       "end": {
    //         "dateTime": "2019-11-15T22:00:00.0000000",
    //         "timeZone": "UTC"
    //       },
    //       "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 i: i32 = 0;
    var odata_etag: [:0]const u8 = "";
    var id: [:0]const u8 = "";
    var created_date_time: [:0]const u8 = "";
    var last_modified_date_time: [:0]const u8 = "";
    var change_key: [:0]const u8 = "";
    var original_start_time_zone: [:0]const u8 = "";
    var original_end_time_zone: [:0]const u8 = "";
    var i_cal_u_id: [:0]const u8 = "";
    var reminder_minutes_before_start: i32 = 0;
    var is_reminder_on: bool = false;
    var has_attachments: bool = false;
    var subject: [:0]const u8 = "";
    var body_preview: [:0]const u8 = "";
    var importance: [:0]const u8 = "";
    var sensitivity: [:0]const u8 = "";
    var is_all_day: bool = false;
    var is_cancelled: bool = false;
    var is_organizer: bool = false;
    var response_requested: bool = false;
    var series_master_id: [:0]const u8 = "";
    var show_as: [:0]const u8 = "";
    var v_type: [:0]const u8 = "";
    var web_link: [:0]const u8 = "";
    var online_meeting_url: [:0]const u8 = "";
    var recurrence: [:0]const u8 = "";
    var response_status_response: [:0]const u8 = "";
    var response_status_time: [:0]const u8 = "";
    var body_content_type: [:0]const u8 = "";
    var body_content: [:0]const u8 = "";
    var start_date_time: [:0]const u8 = "";
    var start_time_zone: [:0]const u8 = "";
    var end_date_time: [:0]const u8 = "";
    var end_time_zone: [:0]const u8 = "";
    var location_display_name: [:0]const u8 = "";
    var location_location_type: [:0]const u8 = "";
    var location_unique_id: [:0]const u8 = "";
    var location_unique_id_type: [:0]const u8 = "";
    var organizer_email_address_name: [:0]const u8 = "";
    var organizer_email_address_address: [:0]const u8 = "";
    var j: i32 = 0;
    var count_j: i32 = 0;
    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\"");
    i = 0;
    const count_i = json.sizeOfArray("value");
    while (i < count_i) {
        json.setI(i);
        odata_etag = try json.stringOf(alloc, "value[i].\"@odata.etag\"");
        id = try json.stringOf(alloc, "value[i].id");
        created_date_time = try json.stringOf(alloc, "value[i].createdDateTime");
        last_modified_date_time = try json.stringOf(alloc, "value[i].lastModifiedDateTime");
        change_key = try json.stringOf(alloc, "value[i].changeKey");
        original_start_time_zone = try json.stringOf(alloc, "value[i].originalStartTimeZone");
        original_end_time_zone = try json.stringOf(alloc, "value[i].originalEndTimeZone");
        i_cal_u_id = try json.stringOf(alloc, "value[i].iCalUId");
        reminder_minutes_before_start = json.intOf("value[i].reminderMinutesBeforeStart");
        is_reminder_on = json.boolOf("value[i].isReminderOn");
        has_attachments = json.boolOf("value[i].hasAttachments");
        subject = try json.stringOf(alloc, "value[i].subject");
        body_preview = try json.stringOf(alloc, "value[i].bodyPreview");
        importance = try json.stringOf(alloc, "value[i].importance");
        sensitivity = try json.stringOf(alloc, "value[i].sensitivity");
        is_all_day = json.boolOf("value[i].isAllDay");
        is_cancelled = json.boolOf("value[i].isCancelled");
        is_organizer = json.boolOf("value[i].isOrganizer");
        response_requested = json.boolOf("value[i].responseRequested");
        series_master_id = try json.stringOf(alloc, "value[i].seriesMasterId");
        show_as = try json.stringOf(alloc, "value[i].showAs");
        v_type = try json.stringOf(alloc, "value[i].type");
        web_link = try json.stringOf(alloc, "value[i].webLink");
        online_meeting_url = try json.stringOf(alloc, "value[i].onlineMeetingUrl");
        recurrence = try json.stringOf(alloc, "value[i].recurrence");
        response_status_response = try json.stringOf(alloc, "value[i].responseStatus.response");
        response_status_time = try json.stringOf(alloc, "value[i].responseStatus.time");
        body_content_type = try json.stringOf(alloc, "value[i].body.contentType");
        body_content = try json.stringOf(alloc, "value[i].body.content");
        start_date_time = try json.stringOf(alloc, "value[i].start.dateTime");
        start_time_zone = try json.stringOf(alloc, "value[i].start.timeZone");
        end_date_time = try json.stringOf(alloc, "value[i].end.dateTime");
        end_time_zone = try json.stringOf(alloc, "value[i].end.timeZone");
        location_display_name = try json.stringOf(alloc, "value[i].location.displayName");
        location_location_type = try json.stringOf(alloc, "value[i].location.locationType");
        location_unique_id = try json.stringOf(alloc, "value[i].location.uniqueId");
        location_unique_id_type = try json.stringOf(alloc, "value[i].location.uniqueIdType");
        organizer_email_address_name = try json.stringOf(alloc, "value[i].organizer.emailAddress.name");
        organizer_email_address_address = try json.stringOf(alloc, "value[i].organizer.emailAddress.address");
        j = 0;
        count_j = json.sizeOfArray("value[i].categories");
        while (j < count_j) {
            json.setJ(j);
            // ...
            j = j + 1;
        }

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

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

        i = i + 1;
    }

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