private async Task<bool> SendCreateNotebookRequest(HttpRequestMessage createMessage,clsOneNoteNotebook newNotebook, StringBuilder sbMessage) { bool result = false; var httpClient = new HttpClient(); CreateNotebookResponse 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 ParseCreateNotebookResponse(response); // Update the UI accordingly if (UpdateUIAfterCreateRequest(response) == false) { sbMessage.AppendLine("Error: " + response.StatusCode); return false; } } catch (Exception ex) { sbMessage.AppendLine(ex.Message); return false; } if (_response as CreateNotebookResponse != null) { successResponse = (CreateNotebookResponse)_response; } else { return false; } if (successResponse.StatusCode != HttpStatusCode.Created) { return false; } else { newNotebook.id = successResponse.id; newNotebook.name = successResponse.name; newNotebook.sectionsUrl = successResponse.sectionsUrl; newNotebook.oneNoteClientUrl = successResponse.oneNoteClientUrl; newNotebook.oneNoteWebUrl = successResponse.oneNoteWebUrl; result = true; } return result; }
private async Task<bool> CreateNoteBook(clsOneNoteNotebook newNotebook, StringBuilder sbMessage) { bool result = false; String Body = "{ name: \"" + newNotebook.name +"\" }";// +; HttpRequestMessage createMessage = new HttpRequestMessage(HttpMethod.Post, NotebooksEndpoint) { Content = new StringContent(Body, System.Text.Encoding.UTF8, "application/json") }; try { result = await SendCreateNotebookRequest(createMessage, newNotebook,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; } }
public async Task<bool> GetNoteBook(clsOneNoteNotebook Notebook, StringBuilder sbMessage) { bool result = false; NotebookListResponse myNotebookListResponse; clsOneNoteNotebook temp = new clsOneNoteNotebook {name = NotebookName }; int IndexInList; string strRequest; strRequest = NotebooksEndpoint;// +"?$filter=contains(name,'" + tmpNotebook.name + "')"; var NoteBookListMessage = 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 try { HttpResponseMessage response = await httpClient.SendAsync(NoteBookListMessage); myNotebookListResponse = await ParseNotebookListResponse(response); } catch (Exception ex) { sbMessage.AppendLine(ex.Message); return false; } if (myNotebookListResponse.Notebooks == null || myNotebookListResponse.Notebooks.Count == 0) { //we didn't find any notebooks so we need to create ours result = await CreateNoteBook(Notebook,sbMessage); } else { IndexInList = myNotebookListResponse.Notebooks.IndexOf(temp); if (IndexInList >= 0 ) { Notebook.id = myNotebookListResponse.Notebooks[IndexInList].id; Notebook.name = myNotebookListResponse.Notebooks[IndexInList].name; Notebook.sectionsUrl = myNotebookListResponse.Notebooks[IndexInList].sectionsUrl; result = true; } else { //our notebook is not on the list so we need to create it result = await CreateNoteBook(Notebook, sbMessage); } } return result; }
public clsOneNote() { this.CreateButtonEnabled = false; this.InfoText = string.Empty; this.SignedInMsg = "Not Signed In"; this.IsSignedIn = false; this.IsHyperLinkViewNoteVisible = false; this.SectionName = cSectionNameHint; this.NotebookName = cNotebookNameHint; myNoteBook = new clsOneNoteNotebook(); SectionList = new List<clsOneNoteSection>(); }