PureBasic
PureBasic
OneNote - Create Page
See more OneNote Examples
Creates a new OneNote page with a rendered image and an attached PDF.Chilkat PureBasic Downloads
IncludeFile "CkBinData.pb"
IncludeFile "CkDateTime.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkHttpRequest.pb"
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkStringBuilder.pb"
Procedure ChilkatExample()
success.i = 0
; This example assumes the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
http.i = CkHttp::ckCreate()
If http.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; 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:
req.i = CkHttpRequest::ckCreate()
If req.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; 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"
CkHttpRequest::setCkPath(req, "/v1.0/me/onenote/sections/0-3A33FCEB9B74CC15!20350/pages")
; We'll be sending a POST.
CkHttpRequest::setCkHttpVerb(req, "POST")
; The Content-Type is multipart/form-data
; Chilkat will automatically generate a boundary string.
CkHttpRequest::setCkContentType(req, "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.
; ------------------------------
sbHtml.i = CkStringBuilder::ckCreate()
If sbHtml.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
bCrlf.i = 1
CkStringBuilder::ckAppendLine(sbHtml,"<!DOCTYPE html>",bCrlf)
CkStringBuilder::ckAppendLine(sbHtml,"<html>",bCrlf)
CkStringBuilder::ckAppendLine(sbHtml," <head>",bCrlf)
CkStringBuilder::ckAppendLine(sbHtml," <title>A page with <i>rendered</i> images and an <b>attached</b> file</title>",bCrlf)
CkStringBuilder::ckAppendLine(sbHtml," <meta name=" + Chr(34) + "created" + Chr(34) + " content=" + Chr(34) + "TIMESTAMP_CURRENT" + Chr(34) + " />",bCrlf)
CkStringBuilder::ckAppendLine(sbHtml," </head>",bCrlf)
CkStringBuilder::ckAppendLine(sbHtml," <body>",bCrlf)
CkStringBuilder::ckAppendLine(sbHtml," <p>Here's an image from an online source:</p>",bCrlf)
CkStringBuilder::ckAppendLine(sbHtml," <img src=" + Chr(34) + "https://www.chilkatsoft.com/images/starfish.jpg" + Chr(34) + " alt=" + Chr(34) + "an image on the page" + Chr(34) + " width=" + Chr(34) + "500" + Chr(34) + " />",bCrlf)
CkStringBuilder::ckAppendLine(sbHtml," <p>Here's an image uploaded as binary data:</p>",bCrlf)
CkStringBuilder::ckAppendLine(sbHtml," <img src=" + Chr(34) + "name:imageBlock1" + Chr(34) + " alt=" + Chr(34) + "an image on the page" + Chr(34) + " width=" + Chr(34) + "300" + Chr(34) + " />",bCrlf)
CkStringBuilder::ckAppendLine(sbHtml," <p>Here's a file attachment:</p>",bCrlf)
CkStringBuilder::ckAppendLine(sbHtml," <object data-attachment=" + Chr(34) + "FileName.pdf" + Chr(34) + " data=" + Chr(34) + "name:fileBlock1" + Chr(34) + " type=" + Chr(34) + "application/pdf" + Chr(34) + " />",bCrlf)
CkStringBuilder::ckAppendLine(sbHtml," </body>",bCrlf)
CkStringBuilder::ckAppendLine(sbHtml,"</html>",bCrlf)
dtNow.i = CkDateTime::ckCreate()
If dtNow.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkDateTime::ckSetFromCurrentSystemTime(dtNow)
numReplaced.i = CkStringBuilder::ckReplace(sbHtml,"TIMESTAMP_CURRENT",CkDateTime::ckGetAsTimestamp(dtNow,1))
CkHttpRequest::ckAddStringForUpload2(req,"Presentation","",CkStringBuilder::ckGetAsString(sbHtml),"utf-8","text/html")
; ------------------------------
; Add the JPG image.
; ------------------------------
success = CkHttpRequest::ckAddFileForUpload2(req,"imageBlock1","qa_data/jpg/penguins2.jpg","image/jpeg")
If success = 0
Debug CkHttpRequest::ckLastErrorText(req)
CkHttp::ckDispose(http)
CkHttpRequest::ckDispose(req)
CkStringBuilder::ckDispose(sbHtml)
CkDateTime::ckDispose(dtNow)
ProcedureReturn
EndIf
; ------------------------------
; Add the PDF attachment.
; ------------------------------
bdPdf.i = CkBinData::ckCreate()
If bdPdf.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkBinData::ckLoadFile(bdPdf,"qa_data/pdf/helloWorld.pdf")
If success = 0
Debug "Failed to load PDF file."
CkHttp::ckDispose(http)
CkHttpRequest::ckDispose(req)
CkStringBuilder::ckDispose(sbHtml)
CkDateTime::ckDispose(dtNow)
CkBinData::ckDispose(bdPdf)
ProcedureReturn
EndIf
success = CkHttpRequest::ckAddBdForUpload(req,"fileBlock1","FileName.pdf",bdPdf,"application/pdf")
If success = 0
Debug CkHttpRequest::ckLastErrorText(req)
CkHttp::ckDispose(http)
CkHttpRequest::ckDispose(req)
CkStringBuilder::ckDispose(sbHtml)
CkDateTime::ckDispose(dtNow)
CkBinData::ckDispose(bdPdf)
ProcedureReturn
EndIf
; Adds the "Authorization: Bearer ACCESS_TOKEN" header.
CkHttp::setCkAuthToken(http, "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).
resp.i = CkHttpResponse::ckCreate()
If resp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkHttp::ckHttpSReq(http,"graph.microsoft.com",443,1,req,resp)
If success = 0
Debug CkHttp::ckLastErrorText(http)
CkHttp::ckDispose(http)
CkHttpRequest::ckDispose(req)
CkStringBuilder::ckDispose(sbHtml)
CkDateTime::ckDispose(dtNow)
CkBinData::ckDispose(bdPdf)
CkHttpResponse::ckDispose(resp)
ProcedureReturn
EndIf
sbResponseBody.i = CkStringBuilder::ckCreate()
If sbResponseBody.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttpResponse::ckGetBodySb(resp,sbResponseBody)
jResp.i = CkJsonObject::ckCreate()
If jResp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckLoadSb(jResp,sbResponseBody)
CkJsonObject::setCkEmitCompact(jResp, 0)
Debug "Response status code: " + Str(CkHttpResponse::ckStatusCode(resp))
Debug "Response Body:"
Debug CkJsonObject::ckEmit(jResp)
respStatusCode.i = CkHttpResponse::ckStatusCode(resp)
Debug "Response Status Code = " + Str(respStatusCode)
If respStatusCode >= 400
Debug "Response Header:"
Debug CkHttpResponse::ckHeader(resp)
Debug "Failed."
CkHttp::ckDispose(http)
CkHttpRequest::ckDispose(req)
CkStringBuilder::ckDispose(sbHtml)
CkDateTime::ckDispose(dtNow)
CkBinData::ckDispose(bdPdf)
CkHttpResponse::ckDispose(resp)
CkStringBuilder::ckDispose(sbResponseBody)
CkJsonObject::ckDispose(jResp)
ProcedureReturn
EndIf
; 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§ion-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
odata_context.s = CkJsonObject::ckStringOf(jResp,Chr(34) + "@odata.context" + Chr(34))
id.s = CkJsonObject::ckStringOf(jResp,"id")
self.s = CkJsonObject::ckStringOf(jResp,"self")
createdDateTime.s = CkJsonObject::ckStringOf(jResp,"createdDateTime")
title.s = CkJsonObject::ckStringOf(jResp,"title")
createdByAppId.s = CkJsonObject::ckStringOf(jResp,"createdByAppId")
contentUrl.s = CkJsonObject::ckStringOf(jResp,"contentUrl")
lastModifiedDateTime.s = CkJsonObject::ckStringOf(jResp,"lastModifiedDateTime")
linksOneNoteClientUrlHref.s = CkJsonObject::ckStringOf(jResp,"links.oneNoteClientUrl.href")
linksOneNoteWebUrlHref.s = CkJsonObject::ckStringOf(jResp,"links.oneNoteWebUrl.href")
CkHttp::ckDispose(http)
CkHttpRequest::ckDispose(req)
CkStringBuilder::ckDispose(sbHtml)
CkDateTime::ckDispose(dtNow)
CkBinData::ckDispose(bdPdf)
CkHttpResponse::ckDispose(resp)
CkStringBuilder::ckDispose(sbResponseBody)
CkJsonObject::ckDispose(jResp)
ProcedureReturn
EndProcedure