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

Outlook -- Copy Mail Folder and Contents to another Mail Folder

See more Outlook Examples

Copy a mailfolder and its contents to another mailfolder.

This example applies to: Exchange Online | Office 365 | Hotmail.com | Live.com | MSN.com | Outlook.com | Passport.com

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 copy /Inbox/abc/subFolderA and its contents to /Inbox/xyz
    // We'll need the folder ids for both source and destination folders..

    // Get the folder IDs 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 {};

    // Get the IDs for the source and destination folders.
    const src_folder_id = ht_folder_map.lookupStr(alloc, "/Inbox/abc/subFolderA") catch {
        std.debug.print("Folder ID not found\n", .{});
        return;
    };

    const dst_folder_id = ht_folder_map.lookupStr(alloc, "/Inbox/xyz") catch {
        std.debug.print("Destination folder ID not found\n", .{});
        return;
    };

    // Create a JSON request body with this content:
    //
    //         {
    //           "DestinationId": "destinationId-value"
    //         }

    const json_request_body = try chilkat.JsonObject.init();
    defer json_request_body.deinit();
    json_request_body.updateString("DestinationId", dst_folder_id) catch {};

    http.setUrlVar("src_folder_id", src_folder_id) catch {};

    // Copy the source folder to the new location
    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", "https://graph.microsoft.com/v1.0/me/mailFolders/{$src_folder_id}/copy", json_request_body, "application/json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    // A 201 response indicates success.
    if (resp.getStatusCode() == 201) {
        std.debug.print("Folder copied.\n", .{});
    } else {
        std.debug.print("Response status code = {d}\n", .{resp.getStatusCode()});
        std.debug.print("Error: Folder not copied.\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#mailFolder",
    //       "@odata.type": "#microsoft.graph.mailFolder",
    //       "id": "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAM6JqMAAAAA=",
    //       "displayName": "subFolderA",
    //       "parentFolderId": "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwEAAAA=",
    //       "childFolderCount": 1,
    //       "unreadItemCount": 0,
    //       "totalItemCount": 0
    //     }
}