Rust
Rust
Outlook -- Create Map of Folder Paths to IDs
See more Outlook Examples
Recursively descends folders and creates a hashtable of folder paths to IDs. This can be done once at the start of your program (or even less if the map is persisted to a file or database).The reason this is necessary is because folder ID's need to be passed to the Outlook API, and an application will typically be working with folder paths.
Note: This example requires Chilkat v9.5.0.67 or greater.
This example applies to: Exchange Online | Office 365 | Hotmail.com | Live.com | MSN.com | Outlook.com | Passport.com
Chilkat Rust Downloads
let mut success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = chilkat::Http::new();
// Our folder path --> ID map will be stored in this hash table.
let folder_map = chilkat::Hashtable::new();
// Use your previously obtained access token here:
// See the following examples for getting an access token:
// Get Microsoft Graph OAuth2 Access Token (Azure AD v2.0 Endpoint).
// Get Microsoft Graph OAuth2 Access Token (Azure AD Endpoint).
// Refresh Access Token (Azure AD v2.0 Endpoint).
// Refresh Access Token (Azure AD Endpoint).
http.set_auth_token("MICROSOFT_GRAPH_ACCESS_TOKEN");
let sb_response = chilkat::StringBuilder::new();
// Begin by getting the top-level folders.
http.clear_url_vars();
let _ = http.set_url_var("userPrincipalName", "chilkatsoft@outlook.com");
success = http.quick_get_sb("https://graph.microsoft.com/v1.0/users/{$userPrincipalName}/mailFolders", &sb_response).is_ok();
if (!success) && (http.last_status() == 0) {
println!("{}", http.last_error_text());
return;
}
let json = chilkat::JsonObject::new();
let _ = json.load_sb(&sb_response);
json.set_emit_compact(false);
println!("Status code = {}", http.last_status());
if http.last_status() != 200 {
println!("{}", json.emit().unwrap_or_default());
println!("Failed.");
}
// This is our queue/stack of unprocessed folder ID's
// The recursive nature of this example is that we get the
// child folders for each folder ID in the idQueue, which may
// cause additional ID's to be added. We continue until the idQueue
// is empty.
let id_queue = chilkat::StringArray::new();
let sb_folder_path = chilkat::StringBuilder::new();
let sb_queue_entry = chilkat::StringBuilder::new();
let mut folder_name = String::new();
let mut folder_path = String::new();
let mut folder_id = String::new();
// Prime the map and idQueue with the top-level folders.
let mut i = 0;
let mut num_folders = json.size_of_array("value");
while i < num_folders {
json.set_i(i);
folder_name = json.string_of("value[i].displayName").unwrap_or_default();
folder_id = json.string_of("value[i].id").unwrap_or_default();
let _ = sb_folder_path.set_string("/");
let _ = sb_folder_path.append(&folder_name);
folder_path = sb_folder_path.get_as_string().unwrap_or_default();
let _ = folder_map.add_str(&folder_path, &folder_id);
println!("{} --> {}", folder_path, folder_id);
// Push the folder path + id onto the idQueue.
sb_queue_entry.clear();
let _ = sb_queue_entry.set_nth(0, &folder_path, "|", false, false);
let _ = sb_queue_entry.set_nth(1, &folder_id, "|", false, false);
let _ = id_queue.append(&sb_queue_entry.get_as_string().unwrap_or_default());
i = i + 1;
}
// Initial output:
// /Archive --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAG8XunwAAAA=
// /Deleted Items --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEKAAAA
// /Drafts --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEPAAAA
// /Inbox --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEMAAAA
// /Junk Email --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEiAAAA
// /Outbox --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgELAAAA
// /Sent Items --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEJAAAA
//
// Process the idQueue until it becomes empty. This is the recursive loop.
let mut parent_folder_path = String::new();
let mut parent_folder_id = String::new();
while id_queue.length() > 0 {
let _ = sb_queue_entry.set_string(&id_queue.get_string(0).unwrap_or_default());
let _ = id_queue.remove_at(0);
parent_folder_path = sb_queue_entry.get_nth(0, "|", false, false).unwrap_or_default();
parent_folder_id = sb_queue_entry.get_nth(1, "|", false, false).unwrap_or_default();
let _ = http.set_url_var("id", &parent_folder_id);
success = http.quick_get_sb("https://graph.microsoft.com/v1.0/users/{$userPrincipalName}/mailFolders/{$id}/childFolders", &sb_response).is_ok();
if (!success) && (http.last_status() == 0) {
println!("{}", http.last_error_text());
return;
}
let _ = json.load_sb(&sb_response);
if http.last_status() != 200 {
println!("Status code = {}", http.last_status());
println!("{}", json.emit().unwrap_or_default());
println!("Failed.");
}
i = 0;
num_folders = json.size_of_array("value");
while i < num_folders {
json.set_i(i);
folder_name = json.string_of("value[i].displayName").unwrap_or_default();
folder_id = json.string_of("value[i].id").unwrap_or_default();
let _ = sb_folder_path.set_string(&parent_folder_path);
let _ = sb_folder_path.append("/");
let _ = sb_folder_path.append(&folder_name);
folder_path = sb_folder_path.get_as_string().unwrap_or_default();
let _ = folder_map.add_str(&folder_path, &folder_id);
println!("{} --> {}", folder_path, folder_id);
// Push the folder path + id onto the idQueue.
sb_queue_entry.clear();
let _ = sb_queue_entry.set_nth(0, &folder_path, "|", false, false);
let _ = sb_queue_entry.set_nth(1, &folder_id, "|", false, false);
let _ = id_queue.append(&sb_queue_entry.get_as_string().unwrap_or_default());
i = i + 1;
}
}
// The hash table of mail folder paths --> ID's can be persisted to XML and saved to a file or database (or anywhere..)
let sb_folder_map_xml = chilkat::StringBuilder::new();
let _ = folder_map.to_xml_sb(&sb_folder_map_xml);
let _ = sb_folder_map_xml.write_file("qa_data/outlook/folderMap.xml", "utf-8", false);
// The hash table can be restored from the serialized XML like this:
let ht2 = chilkat::Hashtable::new();
let sb2 = chilkat::StringBuilder::new();
let _ = sb2.load_file("qa_data/outlook/folderMap.xml", "utf-8");
let _ = ht2.add_from_xml_sb(&sb2);
// What's the ID for the folder "/Inbox/abc/subFolderA" ?
println!("id for /Inbox/abc/subFolderA = {}", ht2.lookup_str("/Inbox/abc/subFolderA").unwrap_or_default());
// Final output:
// /Archive --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAG8XunwAAAA=
// /Deleted Items --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEKAAAA
// /Drafts --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEPAAAA
// /Inbox --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEMAAAA
// /Junk Email --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEiAAAA
// /Outbox --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgELAAAA
// /Sent Items --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEJAAAA
// /Inbox/abc --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huv8AAAA=
// /Inbox/xyz --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwEAAAA=
// /Inbox/abc/subFolderA --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwAAAQ==
// /Inbox/abc/subFolderB --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwMAAAA=
// /Inbox/abc/subFolderA/a --> AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwIAAAA=
//
// ------------------------------------------------------------------------------------------------------
// This example applies to: Exchange Online | Office 365 | Hotmail.com | Live.com | MSN.com | Outlook.com | Passport.com
//
// The Microsoft Graph Outlook Mail API lets you read, create, and send messages and attachments,
// view and respond to event messages, and manage folders that are secured by Azure Active Directory
// in Office 365. It also provides the same functionality in Microsoft accounts specifically
// in these domains: Hotmail.com, Live.com, MSN.com, Outlook.com, and Passport.com.