Sample code for 30+ languages & platforms
Unicode C++

OneNote - Create Page

See more OneNote Examples

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

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkHttpW.h>
#include <CkHttpRequestW.h>
#include <CkStringBuilderW.h>
#include <CkDateTimeW.h>
#include <CkBinDataW.h>
#include <CkHttpResponseW.h>
#include <CkJsonObjectW.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    CkHttpW http;

    // 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:
    CkHttpRequestW req;

    // 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.put_Path(L"/v1.0/me/onenote/sections/0-3A33FCEB9B74CC15!20350/pages");

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

    // The Content-Type is multipart/form-data
    // Chilkat will automatically generate a boundary string.
    req.put_ContentType(L"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.
    // ------------------------------
    CkStringBuilderW sbHtml;
    bool bCrlf = true;
    sbHtml.AppendLine(L"<!DOCTYPE html>",bCrlf);
    sbHtml.AppendLine(L"<html>",bCrlf);
    sbHtml.AppendLine(L"  <head>",bCrlf);
    sbHtml.AppendLine(L"    <title>A page with <i>rendered</i> images and an <b>attached</b> file</title>",bCrlf);
    sbHtml.AppendLine(L"    <meta name=\"created\" content=\"TIMESTAMP_CURRENT\" />",bCrlf);
    sbHtml.AppendLine(L"  </head>",bCrlf);
    sbHtml.AppendLine(L"  <body>",bCrlf);
    sbHtml.AppendLine(L"    <p>Here's an image from an online source:</p>",bCrlf);
    sbHtml.AppendLine(L"    <img src=\"https://www.chilkatsoft.com/images/starfish.jpg\" alt=\"an image on the page\" width=\"500\" />",bCrlf);
    sbHtml.AppendLine(L"    <p>Here's an image uploaded as binary data:</p>",bCrlf);
    sbHtml.AppendLine(L"    <img src=\"name:imageBlock1\" alt=\"an image on the page\" width=\"300\" />",bCrlf);
    sbHtml.AppendLine(L"    <p>Here's a file attachment:</p>",bCrlf);
    sbHtml.AppendLine(L"    <object data-attachment=\"FileName.pdf\" data=\"name:fileBlock1\" type=\"application/pdf\" />",bCrlf);
    sbHtml.AppendLine(L"  </body>",bCrlf);
    sbHtml.AppendLine(L"</html>",bCrlf);

    CkDateTimeW dtNow;
    dtNow.SetFromCurrentSystemTime();
    int numReplaced = sbHtml.Replace(L"TIMESTAMP_CURRENT",dtNow.getAsTimestamp(true));

    req.AddStringForUpload2(L"Presentation",L"",sbHtml.getAsString(),L"utf-8",L"text/html");

    // ------------------------------
    // Add the JPG image.
    // ------------------------------
    success = req.AddFileForUpload2(L"imageBlock1",L"qa_data/jpg/penguins2.jpg",L"image/jpeg");
    if (success == false) {
        wprintf(L"%s\n",req.lastErrorText());
        return;
    }

    // ------------------------------
    // Add the PDF attachment.
    // ------------------------------
    CkBinDataW bdPdf;
    success = bdPdf.LoadFile(L"qa_data/pdf/helloWorld.pdf");
    if (success == false) {
        wprintf(L"Failed to load PDF file.\n");
        return;
    }

    success = req.AddBdForUpload(L"fileBlock1",L"FileName.pdf",bdPdf,L"application/pdf");
    if (success == false) {
        wprintf(L"%s\n",req.lastErrorText());
        return;
    }

    // Adds the "Authorization: Bearer ACCESS_TOKEN" header.
    http.put_AuthToken(L"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).
    CkHttpResponseW resp;
    success = http.HttpSReq(L"graph.microsoft.com",443,true,req,resp);
    if (success == false) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

    CkStringBuilderW sbResponseBody;
    resp.GetBodySb(sbResponseBody);
    CkJsonObjectW jResp;
    jResp.LoadSb(sbResponseBody);
    jResp.put_EmitCompact(false);

    wprintf(L"Response status code: %d\n",resp.get_StatusCode());
    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",jResp.emit());

    int respStatusCode = resp.get_StatusCode();
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",resp.header());
        wprintf(L"Failed.\n");
        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

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
    // See this example explaining how this memory should be used: const char * functions.

    const wchar_t *odata_context = jResp.stringOf(L"\"@odata.context\"");
    const wchar_t *id = jResp.stringOf(L"id");
    const wchar_t *self = jResp.stringOf(L"self");
    const wchar_t *createdDateTime = jResp.stringOf(L"createdDateTime");
    const wchar_t *title = jResp.stringOf(L"title");
    const wchar_t *createdByAppId = jResp.stringOf(L"createdByAppId");
    const wchar_t *contentUrl = jResp.stringOf(L"contentUrl");
    const wchar_t *lastModifiedDateTime = jResp.stringOf(L"lastModifiedDateTime");
    const wchar_t *linksOneNoteClientUrlHref = jResp.stringOf(L"links.oneNoteClientUrl.href");
    const wchar_t *linksOneNoteWebUrlHref = jResp.stringOf(L"links.oneNoteWebUrl.href");
    }