Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Objective-C) OneNote - Create PageCreates a new OneNote page with a rendered image and an attached PDF. For more information, see https://docs.microsoft.com/en-us/graph/api/section-post-pages?view=graph-rest-1.0
#import <CkoHttp.h> #import <CkoHttpRequest.h> #import <CkoStringBuilder.h> #import <CkoDateTime.h> #import <CkoBinData.h> #import <CkoHttpResponse.h> #import <CkoJsonObject.h> #import <NSString.h> // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. CkoHttp *http = [[CkoHttp alloc] init]; BOOL success; // 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: CkoHttpRequest *req = [[CkoHttpRequest alloc] init]; // 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.Path = @"/v1.0/me/onenote/sections/0-3A33FCEB9B74CC15!20350/pages"; // We'll be sending a POST. req.HttpVerb = @"POST"; // The Content-Type is multipart/form-data // Chilkat will automatically generate a boundary string. 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. // ------------------------------ CkoStringBuilder *sbHtml = [[CkoStringBuilder alloc] init]; BOOL bCrlf = YES; [sbHtml AppendLine: @"<!DOCTYPE html>" crlf: bCrlf]; [sbHtml AppendLine: @"<html>" crlf: bCrlf]; [sbHtml AppendLine: @" <head>" crlf: bCrlf]; [sbHtml AppendLine: @" <title>A page with <i>rendered</i> images and an <b>attached</b> file</title>" crlf: bCrlf]; [sbHtml AppendLine: @" <meta name=\"created\" content=\"TIMESTAMP_CURRENT\" />" crlf: bCrlf]; [sbHtml AppendLine: @" </head>" crlf: bCrlf]; [sbHtml AppendLine: @" <body>" crlf: bCrlf]; [sbHtml AppendLine: @" <p>Here's an image from an online source:</p>" crlf: bCrlf]; [sbHtml AppendLine: @" <img src=\"https://www.chilkatsoft.com/images/starfish.jpg\" alt=\"an image on the page\" width=\"500\" />" crlf: bCrlf]; [sbHtml AppendLine: @" <p>Here's an image uploaded as binary data:</p>" crlf: bCrlf]; [sbHtml AppendLine: @" <img src=\"name:imageBlock1\" alt=\"an image on the page\" width=\"300\" />" crlf: bCrlf]; [sbHtml AppendLine: @" <p>Here's a file attachment:</p>" crlf: bCrlf]; [sbHtml AppendLine: @" <object data-attachment=\"FileName.pdf\" data=\"name:fileBlock1\" type=\"application/pdf\" />" crlf: bCrlf]; [sbHtml AppendLine: @" </body>" crlf: bCrlf]; [sbHtml AppendLine: @"</html>" crlf: bCrlf]; CkoDateTime *dtNow = [[CkoDateTime alloc] init]; [dtNow SetFromCurrentSystemTime]; int numReplaced = [[sbHtml Replace: @"TIMESTAMP_CURRENT" replacement: [dtNow GetAsTimestamp: YES]] intValue]; [req AddStringForUpload2: @"Presentation" filename: @"" strData: [sbHtml GetAsString] charset: @"utf-8" contentType: @"text/html"]; // ------------------------------ // Add the JPG image. // ------------------------------ success = [req AddFileForUpload2: @"imageBlock1" path: @"qa_data/jpg/penguins2.jpg" contentType: @"image/jpeg"]; if (success == NO) { NSLog(@"%@",req.LastErrorText); return; } // ------------------------------ // Add the PDF attachment. // ------------------------------ CkoBinData *bdPdf = [[CkoBinData alloc] init]; success = [bdPdf LoadFile: @"qa_data/pdf/helloWorld.pdf"]; if (success == NO) { NSLog(@"%@",@"Failed to load PDF file."); return; } success = [req AddBdForUpload: @"fileBlock1" remoteFilename: @"FileName.pdf" byteData: bdPdf contentType: @"application/pdf"]; if (success == NO) { NSLog(@"%@",req.LastErrorText); return; } // Adds the "Authorization: Bearer ACCESS_TOKEN" header. 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). CkoHttpResponse *resp = [http SynchronousRequest: @"graph.microsoft.com" port: [NSNumber numberWithInt: 443] ssl: YES req: req]; if (http.LastMethodSuccess == NO) { NSLog(@"%@",http.LastErrorText); return; } CkoStringBuilder *sbResponseBody = [[CkoStringBuilder alloc] init]; [resp GetBodySb: sbResponseBody]; CkoJsonObject *jResp = [[CkoJsonObject alloc] init]; [jResp LoadSb: sbResponseBody]; jResp.EmitCompact = NO; NSLog(@"%@%d",@"Response status code: ",[resp.StatusCode intValue]); NSLog(@"%@",@"Response Body:"); NSLog(@"%@",[jResp Emit]); int respStatusCode = [resp.StatusCode intValue]; NSLog(@"%@%d",@"Response Status Code = ",respStatusCode); if (respStatusCode >= 400) { NSLog(@"%@",@"Response Header:"); NSLog(@"%@",resp.Header); NSLog(@"%@",@"Failed."); 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§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 NSString *odata_context = [jResp StringOf: @"\"@odata.context\""]; NSString *id = [jResp StringOf: @"id"]; NSString *self = [jResp StringOf: @"self"]; NSString *createdDateTime = [jResp StringOf: @"createdDateTime"]; NSString *title = [jResp StringOf: @"title"]; NSString *createdByAppId = [jResp StringOf: @"createdByAppId"]; NSString *contentUrl = [jResp StringOf: @"contentUrl"]; NSString *lastModifiedDateTime = [jResp StringOf: @"lastModifiedDateTime"]; NSString *linksOneNoteClientUrlHref = [jResp StringOf: @"links.oneNoteClientUrl.href"]; NSString *linksOneNoteWebUrlHref = [jResp StringOf: @"links.oneNoteWebUrl.href"]; |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.