private async Task<bool> SendCreateSectionRequest(HttpRequestMessage createMessage,clsOneNoteSection newSection, StringBuilder sbMessage) { bool result = false; var httpClient = new HttpClient(); CreateSectionResponse successResponse; _response = null; // Check if Auth token needs to be refreshed await RefreshAuthTokenIfNeeded(); // Add Authorization header httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken); // Note: API only supports JSON return type. httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); //// Get and parse the HTTP response from the service try { HttpResponseMessage response = await httpClient.SendAsync(createMessage); _response = await ParseCreateSectionResponse(response); } catch (Exception ex) { sbMessage.AppendLine(ex.Message); return false; } if (_response as CreateSectionResponse != null) { successResponse = (CreateSectionResponse)_response; } else { return false; } if (successResponse.StatusCode != HttpStatusCode.Created) { result = false; } else { newSection.id = successResponse.id; newSection.name = successResponse.name; newSection.pagesUrl = successResponse.pagesUrl; result = true; } return result; }
public async Task<bool> GetSection(clsOneNoteNotebook myNotebook, clsOneNoteSection Section, StringBuilder sbMessage) { bool result = false; SectionListResponse mySectionListResponse; clsOneNoteSection temp = new clsOneNoteSection{name = SectionName}; int IndexInList; string strRequest; strRequest = myNotebook.sectionsUrl;// +"?$filter=contains(name,'" + tmpSection.name + "')"; var SectionListMessage = new HttpRequestMessage(HttpMethod.Get, strRequest); var httpClient = new HttpClient(); _response = null; // Check if Auth token needs to be refreshed await RefreshAuthTokenIfNeeded(); // Add Authorization header httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken); // Note: API only supports JSON return type. httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); //// Get and parse the HTTP response from the service //this.InfoText = AppResources.PageCreationRequest; try { HttpResponseMessage response = await httpClient.SendAsync(SectionListMessage); mySectionListResponse = await ParseSectionListResponse(response); } catch (Exception ex) { sbMessage.AppendLine(ex.Message); return false; } if (mySectionListResponse.Sections == null || mySectionListResponse.Sections.Count == 0) { //we didn't find any sections so we need to create ours result = await CreateSection(myNotebook,Section,sbMessage); } else { IndexInList = mySectionListResponse.Sections.IndexOf(temp); if (IndexInList >= 0) { Section.id = mySectionListResponse.Sections[IndexInList].id; Section.name = mySectionListResponse.Sections[IndexInList].name; Section.pagesUrl = mySectionListResponse.Sections[IndexInList].pagesUrl; result = true; } else { //our section is not in the list, we need to create it result = await CreateSection(myNotebook, Section, sbMessage); } } return result; }
private async Task<bool> CreateSection(clsOneNoteNotebook Notebook, clsOneNoteSection newSection,StringBuilder sbMessage) { bool result = false; String Body = "{ name: \"" + newSection.name + "\" }"; HttpRequestMessage createMessage = new HttpRequestMessage(HttpMethod.Post, Notebook.sectionsUrl) { Content = new StringContent(Body, System.Text.Encoding.UTF8, "application/json") }; try { result = await SendCreateSectionRequest(createMessage, newSection,sbMessage); } catch (Exception ex) { sbMessage.AppendLine(ex.Message); return false; } return result; }
/// <summary> /// Create a page in a specific notebook and specific section /// Uses the values of the properties NotebookName and SectionName /// which are databound to the UI /// </summary> public async Task CreateSimpleHtml(string simpleHtml) { StringBuilder sbMessage = new StringBuilder(); bool result = false; clsOneNoteSection tmpSection; clsOneNoteNotebook tmpNotebook; int IndexInList; tmpNotebook = new clsOneNoteNotebook { name = NotebookName }; tmpSection = new clsOneNoteSection { name = SectionName }; CreateButtonEnabled = false; InfoText = "Sending page creation request ..."; //Do we have the correct notebook? if (myNoteBook.name != tmpNotebook.name) { //Get the Notebook and the section result = await GetNoteBook(tmpNotebook,sbMessage); if (result == false) { IsHyperLinkViewNoteVisible = false; sbMessage.AppendLine("Page Creation Failed. Problem with notebook."); InfoText = sbMessage.ToString(); return; } myNoteBook = tmpNotebook; result = await GetSection(myNoteBook, tmpSection,sbMessage); if (result == false) { IsHyperLinkViewNoteVisible = false; sbMessage.AppendLine("Page creation Failed. Problem with section."); InfoText = sbMessage.ToString(); return; } SectionList.Add(tmpSection); } IndexInList = SectionList.IndexOf(tmpSection); //Do we have the correct Section? if (IndexInList < 0) { // we don't have the section we need if (myNoteBook.name != NotebookName) { // we don't have the notebook we need //First get the notebook or create it if it doesn't exist result = await GetNoteBook(tmpNotebook,sbMessage); if (result == false) { IsHyperLinkViewNoteVisible = false; sbMessage.AppendLine("Page Creation Failed. Problem with notebook."); InfoText = sbMessage.ToString(); return; } myNoteBook = tmpNotebook; result = await GetSection(myNoteBook, tmpSection,sbMessage); if (result == false) { IsHyperLinkViewNoteVisible = false; sbMessage.AppendLine("Page creation Failed. Problem with section."); InfoText = sbMessage.ToString(); return; } SectionList.Add(tmpSection); } else { result = await GetSection(myNoteBook, tmpSection,sbMessage); if (result==false) { IsHyperLinkViewNoteVisible = false; sbMessage.AppendLine("Page creation Failed. Problem with section."); InfoText = sbMessage.ToString(); return; } SectionList.Add(tmpSection); } } else { //we already have this section on our list. How do we know it's in the correct notebook? tmpSection = SectionList[IndexInList]; if (tmpSection.pagesUrl == null) { if (tmpNotebook.sectionsUrl == null) { result = await GetNoteBook(tmpNotebook,sbMessage); if (result == false) { IsHyperLinkViewNoteVisible = false; sbMessage.AppendLine("Page Creation Failed. Problem with notebook."); InfoText = sbMessage.ToString(); return; } myNoteBook = tmpNotebook; result = await GetSection(myNoteBook, tmpSection,sbMessage); if (result == false) { IsHyperLinkViewNoteVisible = false; sbMessage.AppendLine("Page creation Failed. Problem with section."); InfoText = sbMessage.ToString(); return; } SectionList.Add(tmpSection); } else { //get our section from our notebook result = await GetSection(myNoteBook, tmpSection,sbMessage); if (result == false) { IsHyperLinkViewNoteVisible = false; sbMessage.AppendLine("Page creation Failed. Problem with section."); InfoText = sbMessage.ToString(); return; } SectionList.Add(tmpSection); } } } // Create the request message, which is a text/html single part in this case // The Service also supports content type multipart/form-data for more complex scenarios string LocationOfNote = tmpSection.pagesUrl;// +"/?pageName=" + pageName; var createMessage = new HttpRequestMessage(HttpMethod.Post, LocationOfNote) { Content = new StringContent(simpleHtml, System.Text.Encoding.UTF8, "text/html") }; result = await SendCreatePageRequest(createMessage,sbMessage); if (result == false) { IsHyperLinkViewNoteVisible = false; sbMessage.AppendLine("Page creation failed"); InfoText = sbMessage.ToString(); return; } }