Sample code for 30+ languages & platforms
Rust

OneNote - Create Page

See more OneNote Examples

Creates a new OneNote page with a rendered image and an attached PDF.

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();

// To create a OneNote page, we want to send an HTTP request like the following:

// POST https://graph.microsoft.com/v1.0/me/onenote/sections/{section_id}/pages
// Content-length: 312
// Content-type: multipart/form-data; boundary=MyPartBoundary198374
// 
// --MyPartBoundary198374
// Content-Disposition:form-data; name="Presentation"
// Content-Type:text/html
// 
// <!DOCTYPE html>
// <html>
//   <head>
//     <title>A page with <i>rendered</i> images and an <b>attached</b> file</title>
//     <meta name="created" content="2015-07-22T09:00:00-08:00" />
//   </head>
//   <body>
//     <p>Here's an image from an online source:</p>
//     <img src="https://..." alt="an image on the page" width="500" />
//     <p>Here's an image uploaded as binary data:</p>
//     <img src="name:imageBlock1" alt="an image on the page" width="300" />
//     <p>Here's a file attachment:</p>
//     <object data-attachment="FileName.pdf" data="name:fileBlock1" type="application/pdf" />
//   </body>
// </html>
// 
// --MyPartBoundary198374
// Content-Disposition:form-data; name="imageBlock1"
// Content-Type:image/jpeg
// 
// ... binary image data ...
// 
// --MyPartBoundary198374
// Content-Disposition:form-data; name="fileBlock1"
// Content-Type:application/pdf
// 
// ... binary file data ...
// 
// --MyPartBoundary198374--

// Build the request in a Chilkat HTTP request object:
let req = chilkat::HttpRequest::new();

// Our URL is https://graph.microsoft.com/v1.0/me/onenote/sections/{section_id}/pages
// The path part of the URL is "/v1.0/me/onenote/sections/{section_id}/pages"
// In this example, our section ID is "0-3A33FCEB9B74CC15!20350"
req.set_path("/v1.0/me/onenote/sections/0-3A33FCEB9B74CC15!20350/pages");

// We'll be sending a POST.
req.set_http_verb("POST");

// The Content-Type is multipart/form-data
// Chilkat will automatically generate a boundary string.
req.set_content_type("multipart/form-data");

// When Chilkat HTTP was written many years ago, multipart requests were primarily for uploads.
// Thus the names of methods that add a multipart section end with "ForUpload".
// The multipart request we wish to build has 3 sections: text/html, image/jpeg, and application/pdf.

// ------------------------------
// Add the text/html part.
// ------------------------------
let sb_html = chilkat::StringBuilder::new();
let b_crlf = true;
let _ = sb_html.append_line("<!DOCTYPE html>", b_crlf);
let _ = sb_html.append_line("<html>", b_crlf);
let _ = sb_html.append_line("  <head>", b_crlf);
let _ = sb_html.append_line("    <title>A page with <i>rendered</i> images and an <b>attached</b> file</title>", b_crlf);
let _ = sb_html.append_line("    <meta name=\"created\" content=\"TIMESTAMP_CURRENT\" />", b_crlf);
let _ = sb_html.append_line("  </head>", b_crlf);
let _ = sb_html.append_line("  <body>", b_crlf);
let _ = sb_html.append_line("    <p>Here's an image from an online source:</p>", b_crlf);
let _ = sb_html.append_line("    <img src=\"https://www.chilkatsoft.com/images/starfish.jpg\" alt=\"an image on the page\" width=\"500\" />", b_crlf);
let _ = sb_html.append_line("    <p>Here's an image uploaded as binary data:</p>", b_crlf);
let _ = sb_html.append_line("    <img src=\"name:imageBlock1\" alt=\"an image on the page\" width=\"300\" />", b_crlf);
let _ = sb_html.append_line("    <p>Here's a file attachment:</p>", b_crlf);
let _ = sb_html.append_line("    <object data-attachment=\"FileName.pdf\" data=\"name:fileBlock1\" type=\"application/pdf\" />", b_crlf);
let _ = sb_html.append_line("  </body>", b_crlf);
let _ = sb_html.append_line("</html>", b_crlf);

let dt_now = chilkat::DateTime::new();
let _ = dt_now.set_from_current_system_time();
let num_replaced = sb_html.replace("TIMESTAMP_CURRENT", &dt_now.get_as_timestamp(true).unwrap_or_default());

let _ = req.add_string_for_upload2("Presentation", "", &sb_html.get_as_string().unwrap_or_default(), "utf-8", "text/html");

// ------------------------------
// Add the JPG image.
// ------------------------------
if req.add_file_for_upload2("imageBlock1", "qa_data/jpg/penguins2.jpg", "image/jpeg").is_err() {
    println!("{}", req.last_error_text());
    return;
}

// ------------------------------
// Add the PDF attachment.
// ------------------------------
let bd_pdf = chilkat::BinData::new();
if bd_pdf.load_file("qa_data/pdf/helloWorld.pdf").is_err() {
    println!("Failed to load PDF file.");
    return;
}

if req.add_bd_for_upload("fileBlock1", "FileName.pdf", &bd_pdf, "application/pdf").is_err() {
    println!("{}", req.last_error_text());
    return;
}

// Adds the "Authorization: Bearer ACCESS_TOKEN" header.
http.set_auth_token("ACCESS_TOKEN");

// POST to https://graph.microsoft.com/v1.0/me/onenote/sections/{section_id}/pages
// The path part of the URL is already specified in the req object.
// We only need to specify the domain and the fact that we're doing "https" (SSL/TLS).
let resp = chilkat::HttpResponse::new();
if http.http_s_req("graph.microsoft.com", 443, true, &req, &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 status code: {}", resp.status_code());
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/sections('0-3A33FCEB9B74CC15%2120350')/pages/$entity",
//   "id": "0-18ac61117a664f7e946bcceaeebd6f57!36-3A33FCEB9B74CC15!20350",
//   "self": "https://graph.microsoft.com/v1.0/users/admin@chilkat.io/onenote/pages/0-18ac61117a664f7e946bcceaeebd6f57!36-3A33FCEB9B74CC15!20350",
//   "createdDateTime": "2020-10-22T19:02:12Z",
//   "title": "A page with rendered images and an attached file",
//   "createdByAppId": "WLID-00000000441C9990",
//   "contentUrl": "https://graph.microsoft.com/v1.0/users/admin@chilkat.io/onenote/pages/0-18ac61117a664f7e946bcceaeebd6f57!36-3A33FCEB9B74CC15!20350/content",
//   "lastModifiedDateTime": "2020-10-23T00:02:13.3254289Z",
//   "links": {
//     "oneNoteClientUrl": {
//       "href": "onenote:https://d.docs.live.net/3a33fceb9b74cc15/Documents/Testing%20Notebook/Ddd.one#A%20page%20with%20rendered%20images%20and%20an%20attached%20file&section-id=9d78c221-486e-45f8-8355-1810e475f6c0&page-id=36cd1982-1ef1-4b11-b5a1-30b3dbc43d05&end"
//     },
//     "oneNoteWebUrl": {
//       "href": "https://onedrive.live.com/redir.aspx?cid=3a33fceb9b74cc15&page=edit&resid=3A33FCEB9B74CC15!20344&parId=3A33FCEB9B74CC15!187&wd=target%28Ddd.one%7C9d78c221-486e-45f8-8355-1810e475f6c0%2FA%20page%20with%20rendered%20images%20and%20an%20attached%20file%7C36cd1982-1ef1-4b11-b5a1-30b3dbc43d05%2F%29"
//     }
//   }
// }

// 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 title = j_resp.string_of("title").unwrap_or_default();
let created_by_app_id = j_resp.string_of("createdByAppId").unwrap_or_default();
let content_url = j_resp.string_of("contentUrl").unwrap_or_default();
let last_modified_date_time = j_resp.string_of("lastModifiedDateTime").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();