public async Async.Task OneNoteAddPageWithMultipart() { try { string title = "OneNoteAddPageMultipart test created this"; string htmlBody = $@"<!DOCTYPE html><html><head><title>{title}</title></head> <body>Generated from the test <p> <img src=""name:imageBlock1"" alt=""an image on the page"" width=""300"" /> </p> </body></html> "; string boundary = "MultiPartBoundary32541"; string contentType = "multipart/form-data; boundary=" + boundary; // Create the presentation part. StringContent presentation = new StringContent(htmlBody); presentation.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data"); presentation.Headers.ContentDisposition.Name = "Presentation"; presentation.Headers.ContentType = new MediaTypeHeaderValue("text/html"); StreamContent image; // Get an image stream. System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter(); var buff = (byte[])converter.ConvertTo(Microsoft.Graph.Test.Properties.Resources.hamilton, typeof(byte[])); using (System.IO.MemoryStream ms = new System.IO.MemoryStream(buff)) { // Create the image part. image = new StreamContent(ms); image.Headers.ContentDisposition = new ContentDispositionHeaderValue(@"form-data"); image.Headers.ContentDisposition.Name = "imageBlock1"; image.Headers.ContentType = new MediaTypeHeaderValue("image/png"); // Put the multiparts together MultipartContent multiPartContent = new MultipartContent("form-data", boundary); multiPartContent.Add(presentation); multiPartContent.Add(image); // Get the multiPart stream and then send the request to add a page using the stream. testPage = await graphClient.Me.Onenote.Sections[firstSectionID].Pages.Request().AddAsync(multiPartContent); } Assert.IsNotNull(testPage, "We didn't deserialize the OnenotePage object."); Assert.IsTrue(testPage.GetType() == typeof(OnenotePage), $"Expected a OnenotePage object. Actual type is {testPage.GetType().ToString()} "); StringAssert.Contains(htmlBody, testPage.Title, "Expected: title returned in the OneNotePage object is found in the source HTML. Actual: not found."); TestPageCleanUp(); } catch (Microsoft.Graph.ServiceException e) { Assert.Fail("Error code: {0}", e.Error.Code); } catch (Exception e) { Assert.Fail("Error code: {0}", e.Message); } }
public async Task OneNoteAddPageWithMultipart() { try { string title = "OneNoteAddPageMultipart test created this"; string htmlBody = $@"<!DOCTYPE html><html><head><title>{title}</title></head> <body>Generated from the test <p> <img src=""name:imageBlock1"" alt=""an image on the page"" width=""300"" /> </p> </body></html> "; string boundary = "MultiPartBoundary32541"; string contentType = "multipart/form-data; boundary=" + boundary; // Create the presentation part. StringContent presentation = new StringContent(htmlBody); presentation.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data"); presentation.Headers.ContentDisposition.Name = "Presentation"; presentation.Headers.ContentType = new MediaTypeHeaderValue("text/html"); StreamContent image; // Get an image stream. using (Stream ms = ResourceHelper.GetResourceAsStream(ResourceHelper.Hamilton)) { // Create the image part. image = new StreamContent(ms); image.Headers.ContentDisposition = new ContentDispositionHeaderValue(@"form-data"); image.Headers.ContentDisposition.Name = "imageBlock1"; image.Headers.ContentType = new MediaTypeHeaderValue("image/png"); // Put the multiparts together MultipartContent multiPartContent = new MultipartContent("form-data", boundary); multiPartContent.Add(presentation); multiPartContent.Add(image); // Get the multiPart stream and then send the request to add a page using the stream. testPage = await graphClient.Me.Onenote.Sections[firstSectionID].Pages.Request().AddAsync(multiPartContent); } Assert.NotNull(testPage); Assert.True(testPage.GetType() == typeof(OnenotePage)); Assert.Contains(testPage.Title, htmlBody); TestPageCleanUp(); } catch (Microsoft.Graph.ServiceException e) { Assert.True(false, $"Error code: {e.Error.Code}"); } catch (Exception e) { Assert.True(false, $"Error code: {e.Message}"); } }
public async Async.Task OneNoteAddPageMultipartWorkaround() { try { // Get the request URL for adding a page. string requestUrl = graphClient.Me.Onenote.Sections[firstSectionID].Pages.Request().RequestUrl; string title = "OneNoteAddPageMultipart test created this"; string htmlBody = $@"<!DOCTYPE html><html><head><title>{title}</title></head> <body>Generated from the test <p> <img src=""name:imageBlock1"" alt=""an image on the page"" width=""300"" /> </p> </body></html>"; string boundary = "MultiPartBoundary32541"; string contentType = "multipart/form-data; boundary=" + boundary; HttpResponseMessage response; // Create the presentation part. StringContent presentation = new StringContent(htmlBody); presentation.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data"); presentation.Headers.ContentDisposition.Name = "Presentation"; presentation.Headers.ContentType = new MediaTypeHeaderValue("text/html"); StreamContent image; System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter(); var buff = (byte[])converter.ConvertTo(Microsoft.Graph.Test.Properties.Resources.hamilton, typeof(byte[])); using (System.IO.MemoryStream ms = new System.IO.MemoryStream(buff)) { // Create the image part. image = new StreamContent(ms); image.Headers.ContentDisposition = new ContentDispositionHeaderValue(@"form-data"); image.Headers.ContentDisposition.Name = "imageBlock1"; image.Headers.ContentType = new MediaTypeHeaderValue("image/png"); // Put the multiparts togeter MultipartContent multiPartContent = new MultipartContent("form-data", boundary); multiPartContent.Add(presentation); multiPartContent.Add(image); // Create the request message and add the content. HttpRequestMessage hrm = new HttpRequestMessage(HttpMethod.Post, requestUrl); hrm.Content = multiPartContent; // Send the request and get the response. response = await graphClient.HttpProvider.SendAsync(hrm); } // Get the OneNote page that we created. if (response.IsSuccessStatusCode) { // Deserialize into OneNotePage object. var content = await response.Content.ReadAsStringAsync(); testPage = graphClient.HttpProvider.Serializer.DeserializeObject <OnenotePage>(content); Assert.IsNotNull(testPage, "We didn't deserialize the OnenotePage object."); Assert.IsTrue(testPage.GetType() == typeof(OnenotePage), $"Expected a OnenotePage object. Actual type is {testPage.GetType().ToString()} "); StringAssert.Contains(testPage.Title, title, "Expected: title matches, Actual: they don't match."); TestPageCleanUp(); } else { throw new ServiceException( new Error { Code = response.StatusCode.ToString(), Message = await response.Content.ReadAsStringAsync() }); } } catch (Microsoft.Graph.ServiceException e) { Assert.Fail("Error code: {0}", e.Error.Code); } catch (Exception e) { Assert.Fail("Error code: {0}", e.Message); } }