Sample code for 30+ languages & platforms
PowerBuilder

OneNote - Create Page

See more OneNote Examples

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

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Http
oleobject loo_Req
oleobject loo_SbHtml
integer li_BCrlf
oleobject loo_DtNow
integer li_NumReplaced
oleobject loo_BdPdf
oleobject loo_Resp
oleobject loo_SbResponseBody
oleobject loo_JResp
integer li_RespStatusCode
string ls_Odata_context
string ls_Id
string ls_Self
string ls_CreatedDateTime
string ls_Title
string ls_CreatedByAppId
string ls_ContentUrl
string ls_LastModifiedDateTime
string ls_LinksOneNoteClientUrlHref
string ls_LinksOneNoteWebUrlHref

li_Success = 0

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

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// 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:
loo_Req = create oleobject
li_rc = loo_Req.ConnectToNewObject("Chilkat.HttpRequest")

// 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"
loo_Req.Path = "/v1.0/me/onenote/sections/0-3A33FCEB9B74CC15!20350/pages"

// We'll be sending a POST.
loo_Req.HttpVerb = "POST"

// The Content-Type is multipart/form-data
// Chilkat will automatically generate a boundary string.
loo_Req.ContentType = "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.
// ------------------------------
loo_SbHtml = create oleobject
li_rc = loo_SbHtml.ConnectToNewObject("Chilkat.StringBuilder")

li_BCrlf = 1
loo_SbHtml.AppendLine("<!DOCTYPE html>",li_BCrlf)
loo_SbHtml.AppendLine("<html>",li_BCrlf)
loo_SbHtml.AppendLine("  <head>",li_BCrlf)
loo_SbHtml.AppendLine("    <title>A page with <i>rendered</i> images and an <b>attached</b> file</title>",li_BCrlf)
loo_SbHtml.AppendLine("    <meta name=~"created~" content=~"TIMESTAMP_CURRENT~" />",li_BCrlf)
loo_SbHtml.AppendLine("  </head>",li_BCrlf)
loo_SbHtml.AppendLine("  <body>",li_BCrlf)
loo_SbHtml.AppendLine("    <p>Here's an image from an online source:</p>",li_BCrlf)
loo_SbHtml.AppendLine("    <img src=~"https://www.chilkatsoft.com/images/starfish.jpg~" alt=~"an image on the page~" width=~"500~" />",li_BCrlf)
loo_SbHtml.AppendLine("    <p>Here's an image uploaded as binary data:</p>",li_BCrlf)
loo_SbHtml.AppendLine("    <img src=~"name:imageBlock1~" alt=~"an image on the page~" width=~"300~" />",li_BCrlf)
loo_SbHtml.AppendLine("    <p>Here's a file attachment:</p>",li_BCrlf)
loo_SbHtml.AppendLine("    <object data-attachment=~"FileName.pdf~" data=~"name:fileBlock1~" type=~"application/pdf~" />",li_BCrlf)
loo_SbHtml.AppendLine("  </body>",li_BCrlf)
loo_SbHtml.AppendLine("</html>",li_BCrlf)

loo_DtNow = create oleobject
li_rc = loo_DtNow.ConnectToNewObject("Chilkat.CkDateTime")

loo_DtNow.SetFromCurrentSystemTime()
li_NumReplaced = loo_SbHtml.Replace("TIMESTAMP_CURRENT",loo_DtNow.GetAsTimestamp(1))

loo_Req.AddStringForUpload2("Presentation","",loo_SbHtml.GetAsString(),"utf-8","text/html")

// ------------------------------
// Add the JPG image.
// ------------------------------
li_Success = loo_Req.AddFileForUpload2("imageBlock1","qa_data/jpg/penguins2.jpg","image/jpeg")
if li_Success = 0 then
    Write-Debug loo_Req.LastErrorText
    destroy loo_Http
    destroy loo_Req
    destroy loo_SbHtml
    destroy loo_DtNow
    return
end if

// ------------------------------
// Add the PDF attachment.
// ------------------------------
loo_BdPdf = create oleobject
li_rc = loo_BdPdf.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_BdPdf.LoadFile("qa_data/pdf/helloWorld.pdf")
if li_Success = 0 then
    Write-Debug "Failed to load PDF file."
    destroy loo_Http
    destroy loo_Req
    destroy loo_SbHtml
    destroy loo_DtNow
    destroy loo_BdPdf
    return
end if

li_Success = loo_Req.AddBdForUpload("fileBlock1","FileName.pdf",loo_BdPdf,"application/pdf")
if li_Success = 0 then
    Write-Debug loo_Req.LastErrorText
    destroy loo_Http
    destroy loo_Req
    destroy loo_SbHtml
    destroy loo_DtNow
    destroy loo_BdPdf
    return
end if

// Adds the "Authorization: Bearer ACCESS_TOKEN" header.
loo_Http.AuthToken = "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).
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpSReq("graph.microsoft.com",443,1,loo_Req,loo_Resp)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Req
    destroy loo_SbHtml
    destroy loo_DtNow
    destroy loo_BdPdf
    destroy loo_Resp
    return
end if

loo_SbResponseBody = create oleobject
li_rc = loo_SbResponseBody.ConnectToNewObject("Chilkat.StringBuilder")

loo_Resp.GetBodySb(loo_SbResponseBody)
loo_JResp = create oleobject
li_rc = loo_JResp.ConnectToNewObject("Chilkat.JsonObject")

loo_JResp.LoadSb(loo_SbResponseBody)
loo_JResp.EmitCompact = 0

Write-Debug "Response status code: " + string(loo_Resp.StatusCode)
Write-Debug "Response Body:"
Write-Debug loo_JResp.Emit()

li_RespStatusCode = loo_Resp.StatusCode
Write-Debug "Response Status Code = " + string(li_RespStatusCode)
if li_RespStatusCode >= 400 then
    Write-Debug "Response Header:"
    Write-Debug loo_Resp.Header
    Write-Debug "Failed."
    destroy loo_Http
    destroy loo_Req
    destroy loo_SbHtml
    destroy loo_DtNow
    destroy loo_BdPdf
    destroy loo_Resp
    destroy loo_SbResponseBody
    destroy loo_JResp
    return
end if

// 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

ls_Odata_context = loo_JResp.StringOf("~"@odata.context~"")
ls_Id = loo_JResp.StringOf("id")
ls_Self = loo_JResp.StringOf("self")
ls_CreatedDateTime = loo_JResp.StringOf("createdDateTime")
ls_Title = loo_JResp.StringOf("title")
ls_CreatedByAppId = loo_JResp.StringOf("createdByAppId")
ls_ContentUrl = loo_JResp.StringOf("contentUrl")
ls_LastModifiedDateTime = loo_JResp.StringOf("lastModifiedDateTime")
ls_LinksOneNoteClientUrlHref = loo_JResp.StringOf("links.oneNoteClientUrl.href")
ls_LinksOneNoteWebUrlHref = loo_JResp.StringOf("links.oneNoteWebUrl.href")


destroy loo_Http
destroy loo_Req
destroy loo_SbHtml
destroy loo_DtNow
destroy loo_BdPdf
destroy loo_Resp
destroy loo_SbResponseBody
destroy loo_JResp