msgraph-sdk-java icon indicating copy to clipboard operation
msgraph-sdk-java copied to clipboard

Create Page in OneNote using Java SDK v6.x

Open DMarf opened this issue 1 year ago • 3 comments

My current app successfully creates pages in OneNote using the Java SDK 5.x. I am looking to update to the latest Java SDK 6.x (in the process of also adding support for OneDrive).

I have read through the Microsoft Graph Java SDK v6 Changelog and Upgrade Guide and am obtaining valid tokens. I can't find, however, any documentation or examples of how to create a page in OneNote using the Java SDK 6.x. The article Create onenotePage provides examples for HTTP, CLI, and JavaScript, but is missing the examples for C#, Go, Java, etc. provided for other actions such as Create notebook.

Is it possible to create pages using the Java SDK 6.x? Are there any examples?

I have tried the Kotlin code below in the absence of documentation, but always receive the error "The multi-part payload was malformed". Debugging by enabling okhttp logging leads me to believe that this approach results in posting a serialized version of the OnenotePage object rather than the contents field as required. (I didn't provide my initialization for 'contents' as the whole approach seems invalid.)

val mypage = OnenotePage()
mypage.content = content

mGraphClient!!.me().onenote()
    .pages()
    .withUrl("https://graph.microsoft.com/v1.0/me/onenote/pages?sectionName=MySection")
    .post(mypage) { requestConfiguration ->
        requestConfiguration.headers.add(
            "content-type",
            "multipart/form-data; boundary=MyBoundary1234"
        )
    }

DMarf avatar Jul 15 '24 15:07 DMarf

Thanks for reaching out @DMarf.

The snippet you have is the best we can support now since we're not currently generating query parameters for POST requests.

From a high level check, the SDK writes the value of the OnenotePage content field to a stream directly.

Would you mind clarifying what you mean by the serialization approach being invalid? Whether the error message you specified come from the API? Whether your page content contains the boundary?

Ndiritu avatar Aug 07 '24 12:08 Ndiritu

Thank you for following up @Ndiritu

When I enable okhttp logging it shows my post with the following body: {"@odata.type":"#microsoft.graph.onenotePage","content":"{My Content Here}"}

The okhttp logging shows the following response to that post from the server: <-- 400 https://graph.microsoft.com/v1.0/me/onenote/pages?sectionName=MySection ... {"error":{"code":"20011","message":"The multi-part payload was malformed.","innerError":{"date":"2024-08-26T13:58:55","request-id":"17e87792-ff29-4d74-821f-4551a621730b","client-request-id":"8f00a619-c815-4524-8ea3-5bf38cad5253"}}} <-- END HTTP (231-byte body)

I can confirm that my page content does contain the boundary.

I have been able to work around the problem using the following code with the same content that fails above as follows:

val mypage = OnenotePage()

val requestInformation = mGraphClient!!.me().onenote()
    .withUrl("https://graph.microsoft.com/v1.0/me/onenote/pages?sectionName=MySection")
    .pages()
    .toPostRequestInformation(mypage) { requestConfiguration ->
        requestConfiguration.headers.add(
            "Content-type",
            "multipart/form-data; boundary=MyBoundary1234"
        )
    }

requestInformation.content = content.inputStream()

val result = mGraphClient!!.requestAdapter.sendPrimitive(
    requestInformation,
    null,
    InputStream::class.java
)

With this code the okhttp logging shows my content being sent directly in the body of the post rather then being included in the "content" field of a serialized version of OnennotePage and my content successfully appears in OneNote. Is this the proper way the SDK is meant to be used?

By the approach being invalid, I meant that I wasn't sure if a page could be uploaded in the manner I expected. The SDK posts something else (the serialized page) versus what is required by the server (the page contents directly).

DMarf avatar Aug 26 '24 15:08 DMarf

Thank you for clarifying @DMarf. Yes, this points to an issue in the SDK. Glad you were able to find a workaround.

Ndiritu avatar Sep 03 '24 11:09 Ndiritu