Sample code for 30+ languages & platforms
Rust

OneNote - Create Notebook

See more OneNote Examples

Creates a new notebook in Microsoft OneNote

Chilkat Rust Downloads

Rust

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let http = chilkat::Http::new();

// Implements the following CURL command:

// curl -X POST https://graph.microsoft.com/v1.0/me/onenote/notebooks \
//   -H 'authorization: Bearer ACCESS_TOKEN' 
//   -H "Content-type: application/json" \
//   -d '{
//   "displayName": "Notebook name"
// }'

// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code

// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON

// The following JSON is sent in the request body.

// {
//   "displayName": "Testing Notebook"
// }

let json = chilkat::JsonObject::new();
let _ = json.update_string("displayName", "Testing Notebook");

http.set_request_header("Content-type", "application/json");
// Adds the "Authorization: Bearer ACCESS_TOKEN" header.
http.set_auth_token("ACCESS_TOKEN");

let resp = chilkat::HttpResponse::new();
if http.http_json("POST", "https://graph.microsoft.com/v1.0/me/onenote/notebooks", &json, "application/json", &resp).is_err() {
    println!("{}", http.last_error_text());
    return;
}

let sb_response_body = chilkat::StringBuilder::new();
let _ = resp.get_body_sb(&sb_response_body);
let j_resp = chilkat::JsonObject::new();
let _ = j_resp.load_sb(&sb_response_body);
j_resp.set_emit_compact(false);

println!("Response Body:");
println!("{}", j_resp.emit().unwrap_or_default());

let resp_status_code = resp.status_code();
println!("Response Status Code = {}", resp_status_code);
if resp_status_code >= 400 {
    println!("Response Header:");
    println!("{}", resp.header());
    println!("Failed.");
    return;
}

// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)

// {
//   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('admin%40chilkat.io')/onenote/notebooks/$entity",
//   "id": "0-3A33FCEB9B74CC15!20344",
//   "self": "https://graph.microsoft.com/v1.0/users/admin@chilkat.io/onenote/notebooks/0-3A33FCEB9B74CC15!20344",
//   "createdDateTime": "2020-10-22T21:59:03.137Z",
//   "displayName": "Testing Notebook",
//   "lastModifiedDateTime": "2020-10-22T21:59:03.137Z",
//   "isDefault": false,
//   "userRole": "Owner",
//   "isShared": false,
//   "sectionsUrl": "https://graph.microsoft.com/v1.0/users/admin@chilkat.io/onenote/notebooks/0-3A33FCEB9B74CC15!20344/sections",
//   "sectionGroupsUrl": "https://graph.microsoft.com/v1.0/users/admin@chilkat.io/onenote/notebooks/0-3A33FCEB9B74CC15!20344/sectionGroups",
//   "createdBy": {
//     "user": {
//       "id": "4193973766868700757",
//       "displayName": "Joe Smith"
//     }
//   },
//   "lastModifiedBy": {
//     "user": {
//       "id": "4193973766868700757",
//       "displayName": "Joe Smith"
//     }
//   },
//   "links": {
//     "oneNoteClientUrl": {
//       "href": "onenote:https://d.docs.live.net/3a33fceb9b74cc15/Documents/Testing%20Notebook"
//     },
//     "oneNoteWebUrl": {
//       "href": "https://onedrive.live.com/redir.aspx?cid=3a33fceb9b74cc15&page=edit&resid=3A33FCEB9B74CC15!20344"
//     }
//   }
// }

// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON

let odata_context = j_resp.string_of("\"@odata.context\"").unwrap_or_default();
let id = j_resp.string_of("id").unwrap_or_default();
let self_ = j_resp.string_of("self").unwrap_or_default();
let created_date_time = j_resp.string_of("createdDateTime").unwrap_or_default();
let display_name = j_resp.string_of("displayName").unwrap_or_default();
let last_modified_date_time = j_resp.string_of("lastModifiedDateTime").unwrap_or_default();
let is_default = j_resp.bool_of("isDefault");
let user_role = j_resp.string_of("userRole").unwrap_or_default();
let is_shared = j_resp.bool_of("isShared");
let sections_url = j_resp.string_of("sectionsUrl").unwrap_or_default();
let section_groups_url = j_resp.string_of("sectionGroupsUrl").unwrap_or_default();
let created_by_user_id = j_resp.string_of("createdBy.user.id").unwrap_or_default();
let created_by_user_display_name = j_resp.string_of("createdBy.user.displayName").unwrap_or_default();
let last_modified_by_user_id = j_resp.string_of("lastModifiedBy.user.id").unwrap_or_default();
let last_modified_by_user_display_name = j_resp.string_of("lastModifiedBy.user.displayName").unwrap_or_default();
let links_one_note_client_url_href = j_resp.string_of("links.oneNoteClientUrl.href").unwrap_or_default();
let links_one_note_web_url_href = j_resp.string_of("links.oneNoteWebUrl.href").unwrap_or_default();