public async Task<string> CreatePageForIncidentAsync(string siteRootDirectory, string sectionId, Incident incident, IEnumerable<FileContent> inspectionPhotos, IEnumerable<Video> incidentVideos) { var templateFile = Path.Combine(siteRootDirectory, @"Templates\IncidentOneNotePage.cshtml"); var template = System.IO.File.ReadAllText(templateFile); var viewBag = new RazorEngine.Templating.DynamicViewBag(); viewBag.AddValue("InspectionPhotos", inspectionPhotos); viewBag.AddValue("IncidentVideos", incidentVideos); var html = RazorEngine.Engine.Razor.RunCompile(template, "IncidentOneNotePage", typeof(Incident), incident, viewBag); var content = new MultipartFormDataContent(); content.Add(new StringContent(html, Encoding.UTF8, "text/html"), "Presentation"); foreach (var image in inspectionPhotos) { var itemContent = new ByteArrayContent(image.Bytes); var contentType = MimeMapping.GetMimeMapping(image.Name); itemContent.Headers.ContentType = new MediaTypeHeaderValue(contentType); content.Add(itemContent, image.Id); } this.pageEndPoint = string.Format("{0}/sections/{1}/pages", serviceBaseUrl, sectionId); var requestMessage = new HttpRequestMessage(HttpMethod.Post, pageEndPoint); requestMessage.Content = content; var responseMessage = await HttpSendAsync(requestMessage, accessToken); if (responseMessage.StatusCode != System.Net.HttpStatusCode.Created) throw new HttpResponseException(responseMessage.StatusCode); var reponseObject = await GetReponseObjectAsync(responseMessage); return (string)reponseObject.links.oneNoteWebUrl.href; }
public async static Task<string> CreatePageForIncidentAsync(GraphServiceClient graphService, string siteRootDirectory, Group group, Section section, Incident incident, IEnumerable<FileContent> inspectionPhotos, IEnumerable<Models.Video> incidentVideos) { var accessToken = AuthenticationHelper.GetGraphAccessTokenAsync(); var templateFile = Path.Combine(siteRootDirectory, @"Templates\IncidentOneNotePage.cshtml"); var template = System.IO.File.ReadAllText(templateFile); var viewBag = new RazorEngine.Templating.DynamicViewBag(); viewBag.AddValue("InspectionPhotos", inspectionPhotos); viewBag.AddValue("IncidentVideos", incidentVideos); var html = RazorEngine.Engine.Razor.RunCompile(template, "IncidentOneNotePage", typeof(Incident), incident, viewBag); var content = new MultipartFormDataContent(); content.Add(new StringContent(html, Encoding.UTF8, "text/html"), "Presentation"); foreach (var image in inspectionPhotos) { var itemContent = new ByteArrayContent(image.Bytes); var contentType = MimeMapping.GetMimeMapping(image.Name); itemContent.Headers.ContentType = new MediaTypeHeaderValue(contentType); content.Add(itemContent, image.Id); } var pageEndPoint = string.Format("{0}groups/{1}/notes/sections/{2}/pages", AADAppSettings.GraphBetaResourceUrl, group.Id, section.id); var requestMessage = new HttpRequestMessage(HttpMethod.Post, pageEndPoint); requestMessage.Content = content; using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await accessToken); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var responseMessage = await client.SendAsync(requestMessage); if (responseMessage.StatusCode != System.Net.HttpStatusCode.Created) throw new HttpResponseException(responseMessage.StatusCode); var payload = await responseMessage.Content.ReadAsStringAsync(); return JObject.Parse(payload)["links"]["oneNoteWebUrl"]["href"].ToString(); } }