Sample code for 30+ languages & platforms
PureBasic

OneNote - Create Notebook

See more OneNote Examples

Creates a new notebook in Microsoft OneNote

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.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

    ; 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"
    ; }

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"displayName","Testing Notebook")

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

    resp.i = CkHttpResponse::ckCreate()
    If resp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckHttpJson(http,"POST","https://graph.microsoft.com/v1.0/me/onenote/notebooks",json,"application/json",resp)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        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 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)
        CkJsonObject::ckDispose(json)
        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/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

    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")
    displayName.s = CkJsonObject::ckStringOf(jResp,"displayName")
    lastModifiedDateTime.s = CkJsonObject::ckStringOf(jResp,"lastModifiedDateTime")
    isDefault.i = CkJsonObject::ckBoolOf(jResp,"isDefault")
    userRole.s = CkJsonObject::ckStringOf(jResp,"userRole")
    isShared.i = CkJsonObject::ckBoolOf(jResp,"isShared")
    sectionsUrl.s = CkJsonObject::ckStringOf(jResp,"sectionsUrl")
    sectionGroupsUrl.s = CkJsonObject::ckStringOf(jResp,"sectionGroupsUrl")
    createdByUserId.s = CkJsonObject::ckStringOf(jResp,"createdBy.user.id")
    createdByUserDisplayName.s = CkJsonObject::ckStringOf(jResp,"createdBy.user.displayName")
    lastModifiedByUserId.s = CkJsonObject::ckStringOf(jResp,"lastModifiedBy.user.id")
    lastModifiedByUserDisplayName.s = CkJsonObject::ckStringOf(jResp,"lastModifiedBy.user.displayName")
    linksOneNoteClientUrlHref.s = CkJsonObject::ckStringOf(jResp,"links.oneNoteClientUrl.href")
    linksOneNoteWebUrlHref.s = CkJsonObject::ckStringOf(jResp,"links.oneNoteWebUrl.href")


    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(json)
    CkHttpResponse::ckDispose(resp)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jResp)


    ProcedureReturn
EndProcedure