Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

Outlook -- Create a Mail Folder

See more Outlook Examples

Creates a new mail folder as a child of an existing mail folder.

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:
    http.setAuthToken("MICROSOFT_GRAPH_ACCESS_TOKEN");

    // This example will create a new mail folder as a child of /Inbox/abc

    // Get the existing folder ID from the folder map created by this example
    const ht_folder_map = try chilkat.Hashtable.init();
    defer ht_folder_map.deinit();
    const sb_map = try chilkat.StringBuilder.init();
    defer sb_map.deinit();
    sb_map.loadFile("qa_data/outlook/folderMap.xml", "utf-8") catch {};
    ht_folder_map.addFromXmlSb(sb_map) catch {};

    const existing_folder_id = ht_folder_map.lookupStr(alloc, "/Inbox/abc") catch {
        std.debug.print("Folder ID not found\n", .{});
        return;
    };

    // Create a JSON request body with this content:
    //
    //     {
    //       "displayName": "displayName-value",
    //     }

    // This example will create /Inbox/abc/subFolderC
    const json_request_body = try chilkat.JsonObject.init();
    defer json_request_body.deinit();
    json_request_body.updateString("displayName", "subFolderC") catch {};

    http.setUrlVar("folder_id", existing_folder_id) catch {};

    // Create the folder "subFolderC" at the specified location.
    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", "https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/childFolders", json_request_body, "application/json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    // A 201 response indicates success.
    if (http.getLastStatus() == 201) {
        std.debug.print("Folder created.\n", .{});
    } else {
        std.debug.print("Response status code = {d}\n", .{resp.getStatusCode()});
        std.debug.print("Error: Folder not created.\n", .{});
    }

    // Show the response in both cases..
    const json_response = try chilkat.JsonObject.init();
    defer json_response.deinit();
    json_response.setEmitCompact(false);
    json_response.load(try resp.getBodyStr(alloc)) catch {};
    std.debug.print("{s}\n", .{try json_response.emit(alloc)});

    // A sample successful JSON response looks like this:

    //     {
    //       "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('me')/mailFolders('AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huv8AAAA%3D')/childFolders/$entity",
    //       "id": "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAM6JqMIAAAA=",
    //       "displayName": "subFolderC",
    //       "parentFolderId": "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huv8AAAA=",
    //       "childFolderCount": 0,
    //       "unreadItemCount": 0,
    //       "totalItemCount": 0
    //     }
}