From 2a50188e5def6dc851dc583a8abe44a100c72c12 Mon Sep 17 00:00:00 2001 From: Diane Diaz Date: Sun, 30 Aug 2015 18:45:57 -0700 Subject: [PATCH 1/2] Add copy notebook, section, and page examples --- .../DataModel/SampleData.json | 48 +++++- .../DataModel/SampleDataSource.cs | 21 ++- .../Notebooks/CopyNotebooksExample.cs | 103 ++++++++++++ .../Notebooks/PostNotebooksExample.cs | 6 +- .../OneNoteApi/Pages/CopyPagesExample.cs | 100 ++++++++++++ .../OneNoteApi/Pages/PostPagesExample.cs | 12 +- .../SectionGroups/PostSectionGroupsExample.cs | 6 +- .../Sections/CopySectionsExample.cs | 146 ++++++++++++++++++ ...erviceSamplesWinUniversal.Shared.projitems | 3 + 9 files changed, 428 insertions(+), 17 deletions(-) create mode 100644 OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Notebooks/CopyNotebooksExample.cs create mode 100644 OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Pages/CopyPagesExample.cs create mode 100644 OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Sections/CopySectionsExample.cs diff --git a/OneNoteServiceSamplesWinUniversal.Shared/DataModel/SampleData.json b/OneNoteServiceSamplesWinUniversal.Shared/DataModel/SampleData.json index c6546e8..569d8e6 100644 --- a/OneNoteServiceSamplesWinUniversal.Shared/DataModel/SampleData.json +++ b/OneNoteServiceSamplesWinUniversal.Shared/DataModel/SampleData.json @@ -305,7 +305,7 @@ "UniqueId": "Group-2-Item-13", "Title": "Create a new section in a section group", "Subtitle": "BETA Only", - "ImagePath": "Assets/MediumGray.png", + "ImagePath": "Assets/DarkGray.png", "Description": "First we get a list of all section groups and then create a new empty section under the selected section group. Please select a specific section group from the drop-down below and also enter the new section's name in the TextBox below", "RequiresInputComboBox1": "true", "RequiresInputComboBox2": "false", @@ -315,7 +315,7 @@ "UniqueId": "Group-2-Item-14", "Title": "Create a new section group in a notebook", "Subtitle": "BETA Only", - "ImagePath": "Assets/MediumGray.png", + "ImagePath": "Assets/LightGray.png", "Description": "First we get a list of all notebooks and then create a new empty section group under the selected notebook. Please select a specific notebook from the drop-down below and also enter the new section group's name in the TextBox below", "RequiresInputComboBox1": "true", "RequiresInputComboBox2": "false", @@ -366,6 +366,50 @@ "RequiresInputTextBox": "false" } ] + }, + { + "UniqueId": "Group-5", + "Title": "Copy Notebooks, Sections, and Pages", + "ImagePath": "Assets/MediumGray.png", + "Description": "Examples of how to copy notebooks, sections, and pages.", + "Items": [ + { + "UniqueId": "Group-5-Item-0", + "Title": "Copy Notebook", + "ImagePath": "Assets/LightGray.png", + "Description": "Choose the notebook you want to copy, and then enter a name for the new notebook.", + "RequiresInputComboBox1": "true", + "RequiresInputComboBox2": "false", + "RequiresInputTextBox": "true" + }, + { + "UniqueId": "Group-5-Item-1", + "Title": "Copy Section to Notebook", + "ImagePath": "Assets/MediumGray.png", + "Description": "Choose the section you want to copy and the destination notebook. Then, enter a name for the new section.", + "RequiresInputComboBox1": "true", + "RequiresInputComboBox2": "true", + "RequiresInputTextBox": "true" + }, + { + "UniqueId": "Group-5-Item-2", + "Title": "Copy Section to Section Group", + "ImagePath": "Assets/DarkGray.png", + "Description": "Choose the section you want to copy and the destination section group. Then, enter a name for the new section.", + "RequiresInputComboBox1": "true", + "RequiresInputComboBox2": "true", + "RequiresInputTextBox": "true" + }, + { + "UniqueId": "Group-5-Item-3", + "Title": "Copy Page to Section", + "ImagePath": "Assets/MediumGray.png", + "Description": "Choose the page you want to copy and the destination section. Then, enter a name for the new page.", + "RequiresInputComboBox1": "true", + "RequiresInputComboBox2": "true", + "RequiresInputTextBox": "true" + } + ] } ] } \ No newline at end of file diff --git a/OneNoteServiceSamplesWinUniversal.Shared/DataModel/SampleDataSource.cs b/OneNoteServiceSamplesWinUniversal.Shared/DataModel/SampleDataSource.cs index ac8de11..00e3ce1 100644 --- a/OneNoteServiceSamplesWinUniversal.Shared/DataModel/SampleDataSource.cs +++ b/OneNoteServiceSamplesWinUniversal.Shared/DataModel/SampleDataSource.cs @@ -254,7 +254,15 @@ public static async Task ExecuteApi(string uniqueId, bool debug, string case "Group-3-Item-0": return await PatchPagesExample.AppendToDefaultOutlineInPageContent(debug, requiredSelectedId, provider, apiEndPoint); case "Group-4-Item-0": - return await DeletePagesExample.DeletePage(debug, requiredSelectedId, provider, apiEndPoint); + return await DeletePagesExample.DeletePage(debug, requiredSelectedId, provider, apiEndPoint); + case "Group-5-Item-0": + return await CopyNotebooksExample.CopyNotebook(debug, requiredSelectedId, requiredInputText, provider, apiEndPoint); + case "Group-5-Item-1": + return await CopyNotebooksExample.CopyNotebook(debug, requiredSelectedId, requiredInputText, provider, apiEndPoint); + case "Group-5-Item-2": + return await CopyNotebooksExample.CopyNotebook(debug, requiredSelectedId, requiredInputText, provider, apiEndPoint); + case "Group-5-Item-3": + return await CopyNotebooksExample.CopyNotebook(debug, requiredSelectedId, requiredInputText, provider, apiEndPoint); } return null; } @@ -298,8 +306,15 @@ public static async Task ExecuteApiPrereq(string uniqueId, AuthProvider return await GetPagesExample.GetAllPages(false, provider, apiEndPoint); case "Group-3-Item-1": case "Group-4-Item-0": - return await GetPagesExample.GetAllPages(false, provider, apiEndPoint); - + return await GetPagesExample.GetAllPages(false, provider, apiEndPoint); + case "Group-5-Item-0": + return await GetNotebooksExample.GetAllNotebooks(false, provider, apiEndPoint); + case "Group-5-Item-1": + return await GetNotebooksExample.GetAllNotebooks(false, provider, apiEndPoint); + case "Group-5-Item-2": + return await GetSectionGroupsExample.GetAllSectionGroups(false, provider, apiEndPoint); + case "Group-5-Item-3": + return await GetSectionsExample.GetAllSections(false, provider, apiEndPoint); } return null; } diff --git a/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Notebooks/CopyNotebooksExample.cs b/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Notebooks/CopyNotebooksExample.cs new file mode 100644 index 0000000..85435b6 --- /dev/null +++ b/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Notebooks/CopyNotebooksExample.cs @@ -0,0 +1,103 @@ +//********************************************************* +// Copyright (c) Microsoft Corporation +// All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the ""License""); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS +// OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED +// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR +// PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. +// +// See the Apache Version 2.0 License for specific language +// governing permissions and limitations under the License. +//********************************************************* +using System.Diagnostics; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Text; +using System.Threading.Tasks; + +namespace OneNoteServiceSamplesWinUniversal.OneNoteApi.Notebooks +{ + /// + /// Class to show an example of copying notebooks via HTTP POST to the CopyNotebook action. + /// If successful, returns a 202 Accepted status code and a Microsoft.OneNote.Api.CopyStatusModel object. + /// + /// + /// NOTE: This action requires the ID of the notebook that you want to copy. + /// NOTE: It is not the goal of this code sample to produce well re-factored, elegant code. + /// You may notice code blocks being duplicated in various places in this project. + /// We have deliberately added these code blocks to allow anyone browsing the sample + /// to easily view all related functionality in near proximity. + /// + /// + /// var client = new HttpClient(); + /// client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + /// client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await Auth.GetAuthToken()); + /// var createMessage = new HttpRequestMessage(HttpMethod.Post, "https://www.onenote.com/api/beta/me/notes/notebooks/{id}/Microsoft.OneNote.Api.CopyNotebook") + /// { + /// Content = new StringContent("{ renameAs: 'New Notebook Name' }", System.Text.Encoding.UTF8, "application/json") + /// }; + /// HttpResponseMessage response = await client.SendAsync(createMessage); + /// + public static class CopyNotebooksExample + { + #region Examples of POST https://www.onenote.com/api/beta/me/notes/notebooks/{id}/Microsoft.OneNote.Api.CopyNotebook + + /// + /// Copy a notebook + /// + /// Run the code under the debugger + /// ID of the notebook to copy + /// Name for the new notebook + /// Live Connect or Azure AD + /// v1.0 or beta path + /// If you don't use the renameAs parameter, the notebook is overridden. + /// The converted HTTP response message + public static async Task CopyNotebook(bool debug, string notebookId, string newNotebookName, AuthProvider provider, string apiRoute) + { + if (debug) + { + Debugger.Launch(); + Debugger.Break(); + } + + var client = new HttpClient(); + + // Note: The API returns OneNote entities as JSON objects. + client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + + // Not adding the Authentication header results in an unauthorized call and the API returns a 401 status code. + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", + await Auth.GetAuthToken(provider)); + + // Prepare an HTTP POST request to the CopyNotebook endpoint of the target notebook. + // The request body content type is application/json. + // *Optional* parameters are: + // - siteCollectionId and siteId: The SharePoint site to copy the notebook to (if you're copying to a site). + // - groupId: The ID of the group to copy to the notebook to (if you're copying to a group). + // - renameAs: The name for the copy of the notebook. Overwrites the target notebook if not provided. + var createMessage = new HttpRequestMessage(HttpMethod.Post, apiRoute + "notebooks/" + notebookId + "/Microsoft.OneNote.Api.CopyNotebook") + { + Content = new StringContent("{ renameAs : '" + newNotebookName + "' }", Encoding.UTF8, "application/json") + }; + + //test no encoding + //get 409 if name includes an apostrophe + + HttpResponseMessage response = await client.SendAsync(createMessage); + + return await HttpUtils.TranslateResponse(response); + } + + #endregion + + } +} diff --git a/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Notebooks/PostNotebooksExample.cs b/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Notebooks/PostNotebooksExample.cs index 3a71e2e..ee8eeac 100644 --- a/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Notebooks/PostNotebooksExample.cs +++ b/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Notebooks/PostNotebooksExample.cs @@ -29,7 +29,7 @@ namespace OneNoteServiceSamplesWinUniversal.OneNoteApi.Notebooks /// /// Class to show a selection of examples creating notebooks via HTTP POST to the OneNote API /// - Creating a new notebook is represented via the POST HTTP verb. - /// - Creating a new notebook represented by the Uri: https://www.onenote.com/api/v1.0/notebooks + /// - Creating a new notebook represented by the Uri: https://www.onenote.com/api/v1.0/me/notes/notebooks /// For more info, see http://dev.onenote.com/docs /// /// @@ -44,7 +44,7 @@ namespace OneNoteServiceSamplesWinUniversal.OneNoteApi.Notebooks /// var client = new HttpClient(); /// client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); /// client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await Auth.GetAuthToken()); - /// var createMessage = new HttpRequestMessage(HttpMethod.Post, "https://www.onenote.com/api/v1.0/notebooks") + /// var createMessage = new HttpRequestMessage(HttpMethod.Post, "https://www.onenote.com/api/v1.0/me/notes/notebooks") /// { /// Content = new StringContent("{name: NewNotebookName }", System.Text.Encoding.UTF8, "application/json") /// }; @@ -52,7 +52,7 @@ namespace OneNoteServiceSamplesWinUniversal.OneNoteApi.Notebooks /// public static class PostNotebooksExample { - #region Examples of POST https://www.onenote.com/api/v1.0/notebooks + #region Examples of POST https://www.onenote.com/api/v1.0/me/notes/notebooks /// /// Create a notebook with a given name diff --git a/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Pages/CopyPagesExample.cs b/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Pages/CopyPagesExample.cs new file mode 100644 index 0000000..a7a30da --- /dev/null +++ b/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Pages/CopyPagesExample.cs @@ -0,0 +1,100 @@ +//********************************************************* +// Copyright (c) Microsoft Corporation +// All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the ""License""); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS +// OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED +// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR +// PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. +// +// See the Apache Version 2.0 License for specific language +// governing permissions and limitations under the License. +//********************************************************* +using System.Diagnostics; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Text; +using System.Threading.Tasks; + +namespace OneNoteServiceSamplesWinUniversal.OneNoteApi.Pages +{ + /// + /// Class to show an example of copying notebooks via HTTP POST to the CopyToSection action. + /// If successful, returns a 202 Accepted status code and a Microsoft.OneNote.Api.CopyStatusModel object. + /// + /// + /// NOTE: This action requires the ID of the page that you want to copy and the ID of the section you want to copy the page to. + /// NOTE: It is not the goal of this code sample to produce well re-factored, elegant code. + /// You may notice code blocks being duplicated in various places in this project. + /// We have deliberately added these code blocks to allow anyone browsing the sample + /// to easily view all related functionality in near proximity. + /// + /// + /// var client = new HttpClient(); + /// client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + /// client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await Auth.GetAuthToken()); + /// var createMessage = new HttpRequestMessage(HttpMethod.Post, "https://www.onenote.com/api/beta/me/notes/pages/{id}/Microsoft.OneNote.Api.CopyToSection") + /// { + /// Content = new StringContent("{"{ id : 'page-id', renameAs: 'New Page Name' }", System.Text.Encoding.UTF8, "application/json") + /// }; + /// HttpResponseMessage response = await client.SendAsync(createMessage); + /// + public static class CopyPagesExample + { + #region Examples of POST https://www.onenote.com/api/beta/me/notes/pages/{id}/Microsoft.OneNote.Api.CopyToSection + + /// + /// Copy a page + /// + /// Run the code under the debugger + /// ID of the page to copy + /// ID of the destination section + /// Name for the new page + /// Live Connect or Azure AD + /// v1.0 or beta path + /// If you don't use the renameAs parameter, the page is overridden. + /// The converted HTTP response message + public static async Task CopyPageToSection(bool debug, string pageId, string sectionId, string newPageName, AuthProvider provider, string apiRoute) + { + if (debug) + { + Debugger.Launch(); + Debugger.Break(); + } + + var client = new HttpClient(); + + // Note: The API returns OneNote entities as JSON objects. + client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + + // Not adding the Authentication header results in an unauthorized call and the API returns a 401 status code. + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", + await Auth.GetAuthToken(provider)); + + // Prepare an HTTP POST request to the CopyToSection endpoint of the target notebook. + // The request body content type is application/json. + // Requires the ID of the section to copy to. *Optional* parameters are: + // - siteCollectionId and siteId: The SharePoint site to copy the notebook to (if you're copying to a site). + // - groupId: The ID of the group to copy to the notebook to (if you're copying to a group). + // - renameAs: The name for the copy of the notebook. Overwrites the target notebook if not provided. + var createMessage = new HttpRequestMessage(HttpMethod.Post, apiRoute + "pages/" + pageId + "/Microsoft.OneNote.Api.CopyToSection") + { + Content = new StringContent("{ id : '" + sectionId + "', renameAs : '" + newPageName + "' }", Encoding.UTF8, "application/json") + }; + HttpResponseMessage response = await client.SendAsync(createMessage); + + return await HttpUtils.TranslateResponse(response); + } + + #endregion + + } +} diff --git a/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Pages/PostPagesExample.cs b/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Pages/PostPagesExample.cs index ebd5a8b..52e5f05 100644 --- a/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Pages/PostPagesExample.cs +++ b/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Pages/PostPagesExample.cs @@ -35,8 +35,8 @@ namespace OneNoteServiceSamplesWinUniversal.OneNoteApi.Pages /// /// Class to show a selection of examples creating pages via HTTP POST to the OneNote API /// - Creating a new page is represented via the POST HTTP verb. - /// - Creating a page in the default location is represented by the Uri: https://www.onenote.com/api/v1.0/pages - /// - Creating a page in the default notebook but under a specific named section is represented by the Uri: https://www.onenote.com/api/v1.0/pages?sectionName={SectionName} + /// - Creating a page in the default location is represented by the Uri: https://www.onenote.com/api/v1.0/me/notes/pages + /// - Creating a page in the default notebook but under a specific named section is represented by the Uri: https://www.onenote.com/api/v1.0/me/notes/pages?sectionName={SectionName} /// - Creating a page in a specific section is represented by the Uri: https://www.onenote.com/api/v1.0/sections/{sectionId}/pages /// For more info, see http://dev.onenote.com/docs /// @@ -53,7 +53,7 @@ namespace OneNoteServiceSamplesWinUniversal.OneNoteApi.Pages /// /// string simpleHtml = "Page Title" + /// "

Hello World!

"; - /// var createMessage = new HttpRequestMessage(HttpMethod.Post, "https://www.onenote.com/api/v1.0/pages") + /// var createMessage = new HttpRequestMessage(HttpMethod.Post, "https://www.onenote.com/api/v1.0/me/notes/pages") /// { /// Content = new StringContent(simpleHtml, System.Text.Encoding.UTF8, "text/HTML") /// }; @@ -61,7 +61,7 @@ namespace OneNoteServiceSamplesWinUniversal.OneNoteApi.Pages /// public static class PostPagesExample { - #region Examples of POST https://www.onenote.com/api/v1.0/pages with different contents + #region Examples of POST https://www.onenote.com/api/v1.0/me/notes/pages with different contents /// /// Create a very simple page with some formatted text. @@ -438,7 +438,7 @@ public static async Task CreatePageWithNoteTags(bool debug, str return await HttpUtils.TranslateResponse(response); } - #region Examples of POST https://www.onenote.com/api/v1.0/pages with auto-extraction of entities + #region Examples of POST https://www.onenote.com/api/v1.0/me/notes/pages with auto-extraction of entities /// /// Create a page with auto-extracted business card on it. @@ -618,7 +618,7 @@ public static async Task CreatePageWithAutoExtractProduct(bool #endregion - #region Example of POST https://www.onenote.com/api/v1.0/pages?sectionName={AppName} + #region Example of POST https://www.onenote.com/api/v1.0/me/notes/pages?sectionName={AppName} /// /// Create a very simple page with some formatted text in a specific section (name provided by caller) diff --git a/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/SectionGroups/PostSectionGroupsExample.cs b/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/SectionGroups/PostSectionGroupsExample.cs index 2f8fd03..d8a307d 100644 --- a/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/SectionGroups/PostSectionGroupsExample.cs +++ b/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/SectionGroups/PostSectionGroupsExample.cs @@ -63,7 +63,7 @@ public static class PostSectionGroupsExample /// /// Run the code under the debugger /// parent notebook's Id - /// name of the section group to create + /// name of the section group to create /// /// /// Create section group using a application/json content type @@ -105,8 +105,8 @@ public static async Task CreateSectionGroupInNotebook(bool debu /// BETA Create a section group with a given name under a given sectionGroupId /// /// Run the code under the debugger - /// parent section group's Id - /// name of the section group to create + /// parent section group's Id + /// name of the section group to create /// /// /// Create section group using a application/json content type diff --git a/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Sections/CopySectionsExample.cs b/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Sections/CopySectionsExample.cs new file mode 100644 index 0000000..fbc3943 --- /dev/null +++ b/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Sections/CopySectionsExample.cs @@ -0,0 +1,146 @@ +//********************************************************* +// Copyright (c) Microsoft Corporation +// All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the ""License""); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS +// OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED +// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR +// PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. +// +// See the Apache Version 2.0 License for specific language +// governing permissions and limitations under the License. +//********************************************************* +using System.Diagnostics; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Text; +using System.Threading.Tasks; + +namespace OneNoteServiceSamplesWinUniversal.OneNoteApi.Sections +{ + /// + /// Class to show examples of copying sections via HTTP POST to the CopyToNotebook and CopyToSectionGroup actions. + /// If successful, returns a 202 Accepted status code and a Microsoft.OneNote.Api.CopyStatusModel object. + /// + /// + /// NOTE: This action requires the ID of the section that you want to copy and the ID of the notebook or sectiongroup that you want to copy to. + /// NOTE: It is not the goal of this code sample to produce well re-factored, elegant code. + /// You may notice code blocks being duplicated in various places in this project. + /// We have deliberately added these code blocks to allow anyone browsing the sample + /// to easily view all related functionality in near proximity. + /// + /// + /// var client = new HttpClient(); + /// client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + /// client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await Auth.GetAuthToken()); + /// var createMessage = new HttpRequestMessage(HttpMethod.Post, "https://www.onenote.com/api/beta/me/notes/sections/{id}/Microsoft.OneNote.Api.CopyToNotebook") + /// { + /// Content = new StringContent("{ renameAs: 'New Section Name' }", System.Text.Encoding.UTF8, "application/json") + /// }; + /// HttpResponseMessage response = await client.SendAsync(createMessage); + /// + public static class CopySectionsExample + { + #region Examples of POST https://www.onenote.com/api/beta/me/notes/sections/{id}/Microsoft.OneNote.Api.CopyToNotebook + + /// + /// Copy a section + /// + /// Run the code under the debugger + /// Id of the section to copy + /// Id of the destination notebook + /// Name for the new section + /// Live Connect or Azure AD + /// v1.0 or beta path + /// If you don't use the renameAs parameter, the section is overridden. + /// The converted HTTP response message + public static async Task CopySectionToNotebook(bool debug, string sectionId, string notebookId, string newSectionName, AuthProvider provider, string apiRoute) + { + if (debug) + { + Debugger.Launch(); + Debugger.Break(); + } + + var client = new HttpClient(); + + // Note: The API returns OneNote entities as JSON objects. + client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + + // Not adding the Authentication header results in an unauthorized call and the API returns a 401 status code. + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", + await Auth.GetAuthToken(provider)); + + // Prepare an HTTP POST request to the CopyToNotebook endpoint of the target section. + // The request body content type is application/json. + // Requires the ID of the notebook to copy to. Optional parameters are: + // - siteCollectionId and siteId: The SharePoint site to copy the section to (if you're copying to a site). + // - groupId: The ID of the group to copy to the section to (if you're copying to a group). + // - renameAs: The name for the copy of the section. Overwrites the target section if not provided. + var createMessage = new HttpRequestMessage(HttpMethod.Post, apiRoute + "sections/" + sectionId + "/Microsoft.OneNote.Api.CopyToNotebook") + { + Content = new StringContent("{ renameAs : '" + newSectionName + "' }", Encoding.UTF8, "application/json") + }; + HttpResponseMessage response = await client.SendAsync(createMessage); + + return await HttpUtils.TranslateResponse(response); + } + + #endregion + + #region Examples of POST https://www.onenote.com/api/beta/me/notes/sections/{id}/Microsoft.OneNote.Api.CopyToSectionGroup + + /// + /// Copy a section to a section group + /// + /// Run the code under the debugger + /// Id of the section to copy + /// Id of the destination section group + /// Name for the new section + /// Live Connect or Azure AD + /// v1.0 or beta path + /// If you don't use the renameAs parameter, the section is overridden. + /// The converted HTTP response message + public static async Task CopySectionToSectionGroup(bool debug, string sectionId, string sectionGroupId, string newSectionName, AuthProvider provider, string apiRoute) + { + if (debug) + { + Debugger.Launch(); + Debugger.Break(); + } + + var client = new HttpClient(); + + // Note: The API returns OneNote entities as JSON objects. + client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + + // Not adding the Authentication header results in an unauthorized call and the API returns a 401 status code. + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", + await Auth.GetAuthToken(provider)); + + // Prepare an HTTP POST request to the CopyToNotebook endpoint of the target section. + // The request body content type is application/json. + // Requires the ID of the section group to copy to. Optional parameters are: + // - siteCollectionId and siteId: The SharePoint site to copy the section to (if you're copying to a site). + // - groupId: The ID of the group to copy to the section to (if you're copying to a group). + // - renameAs: The name for the copy of the section. Overwrites the target section if not provided. + var createMessage = new HttpRequestMessage(HttpMethod.Post, apiRoute + "sections/" + sectionId + "/Microsoft.OneNote.Api.CopyToSectionGroup") + { + Content = new StringContent("{ renameAs : '" + newSectionName + "' }", Encoding.UTF8, "application/json") + }; + HttpResponseMessage response = await client.SendAsync(createMessage); + + return await HttpUtils.TranslateResponse(response); + } + + #endregion + } +} diff --git a/OneNoteServiceSamplesWinUniversal.Shared/OneNoteServiceSamplesWinUniversal.Shared.projitems b/OneNoteServiceSamplesWinUniversal.Shared/OneNoteServiceSamplesWinUniversal.Shared.projitems index da6f07b..780d68d 100644 --- a/OneNoteServiceSamplesWinUniversal.Shared/OneNoteServiceSamplesWinUniversal.Shared.projitems +++ b/OneNoteServiceSamplesWinUniversal.Shared/OneNoteServiceSamplesWinUniversal.Shared.projitems @@ -20,12 +20,14 @@ + + @@ -33,6 +35,7 @@ + From 52ce6d43bb66990f7709c63460376b112bd0d89e Mon Sep 17 00:00:00 2001 From: Diane Diaz Date: Tue, 1 Sep 2015 10:30:48 -0700 Subject: [PATCH 2/2] e2e copy examples Finished copy section and page methods; Added support for RequiresTwoIds to SampleData.json, SampleDataSource.cs, and Windows' ItemPage.xaml.cs. This allows independent selections in the two combo boxes. TODO: add to WindowsPhone project and add GetCopyStatus (?) --- .../DataModel/SampleData.json | 40 +++++++++++++++++ .../DataModel/SampleDataSource.cs | 36 +++++++++++---- .../Notebooks/CopyNotebooksExample.cs | 20 +++++---- .../OneNoteApi/Pages/CopyPagesExample.cs | 22 +++++----- .../Sections/CopySectionsExample.cs | 44 +++++++++++-------- .../ItemPage.xaml.cs | 39 +++++++++++++--- 6 files changed, 149 insertions(+), 52 deletions(-) diff --git a/OneNoteServiceSamplesWinUniversal.Shared/DataModel/SampleData.json b/OneNoteServiceSamplesWinUniversal.Shared/DataModel/SampleData.json index 569d8e6..a8f0956 100644 --- a/OneNoteServiceSamplesWinUniversal.Shared/DataModel/SampleData.json +++ b/OneNoteServiceSamplesWinUniversal.Shared/DataModel/SampleData.json @@ -13,6 +13,7 @@ "Description": "Create a simple page using HTML to describe the page content (under default section)", "RequiresInputComboBox1": "false", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "false" }, { @@ -22,6 +23,7 @@ "Description": "Create a page with some formatted text and an image", "RequiresInputComboBox1": "true", "RequiresInputComboBox2": "true", + "RequiresTwoIds": "false", "RequiresInputTextBox": "false" }, { @@ -31,6 +33,7 @@ "Description": "Create a page with a snapshot of the HTML of a web page on it", "RequiresInputComboBox1": "true", "RequiresInputComboBox2": "true", + "RequiresTwoIds": "false", "RequiresInputTextBox": "false" }, { @@ -40,6 +43,7 @@ "Description": "Create a page with a snapshot of the OneNote.com homepage on it.", "RequiresInputComboBox1": "true", "RequiresInputComboBox2": "true", + "RequiresTwoIds": "false", "RequiresInputTextBox": "false" }, { @@ -49,6 +53,7 @@ "Description": "Create a page with a PDF file attachment rendered", "RequiresInputComboBox1": "true", "RequiresInputComboBox2": "true", + "RequiresTwoIds": "false", "RequiresInputTextBox": "false" }, { @@ -58,6 +63,7 @@ "Description": "Create a page with examples of note tags. For example build a to do list", "RequiresInputComboBox1": "true", "RequiresInputComboBox2": "true", + "RequiresTwoIds": "false", "RequiresInputTextBox": "false" }, { @@ -67,6 +73,7 @@ "Description": "Create a page with a business card info automatically extracted from an image", "RequiresInputComboBox1": "true", "RequiresInputComboBox2": "true", + "RequiresTwoIds": "false", "RequiresInputTextBox": "false" }, { @@ -76,6 +83,7 @@ "Description": "Create a page with a cooking recipe automatically extracted from an example webpage", "RequiresInputComboBox1": "true", "RequiresInputComboBox2": "true", + "RequiresTwoIds": "false", "RequiresInputTextBox": "false" }, { @@ -85,6 +93,7 @@ "Description": "Create a page with a product info automatically extracted from an example amazon.com webpage", "RequiresInputComboBox1": "true", "RequiresInputComboBox2": "true", + "RequiresTwoIds": "false", "RequiresInputTextBox": "false" }, { @@ -94,6 +103,7 @@ "Description": "Create a page under a given section name using the 'sectionName' query parameter. Please enter a section name in the TextBox below. If the named section does not exist, it will be auto-created", "RequiresInputComboBox1": "false", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "true" } ] @@ -111,6 +121,7 @@ "Description": "Queries for all pages in OneNote and returns a paginated list. By default, paging returns the first 20 pages ordered by last modified time descending (default sort order). The @odata.nextLink in the Response or $skip and $top query parameters can be used to retrieve the next set of pages", "RequiresInputComboBox1": "false", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "false" }, { @@ -120,6 +131,7 @@ "Description": "Following the previous example, first we get a paginated list of all pages and then query the metadata for one of the selected pages. Please select a specific page from the drop-down below", "RequiresInputComboBox1": "true", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "false" }, { @@ -129,6 +141,7 @@ "Description": "Query for a paginated list of pages that contain a given title substring. This example shows how to use the $filter=contains(title, '') query parameter. Please enter a case-sensitive title substring in the TextBox below.", "RequiresInputComboBox1": "false", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "true" }, { @@ -138,6 +151,7 @@ "Description": "Since the list of pages returned is paginated, this example shows how to use the $skip=20&$top=3 query parameters to get up to 3 additional pages after the first 20 pages", "RequiresInputComboBox1": "false", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "false" }, { @@ -147,6 +161,7 @@ "Description": "Get a sorted list of pages using the $orderBy=title asc query parameter and only specific metadata in the response using the $select=id,title query parameter. $orderBy can be used to specify your custom sort order", "RequiresInputComboBox1": "false", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "false" }, { @@ -156,6 +171,7 @@ "Description": "Search all pages and return the paginated list of matching pages that contain the given search term (case-insensitive search)", "RequiresInputComboBox1": "false", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "true" }, { @@ -165,6 +181,7 @@ "Description": "First we get a paginated list of all pages and then return back the content of one of the selected pages as HTML", "RequiresInputComboBox1": "true", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "false" }, { @@ -174,6 +191,7 @@ "Description": "Queries for all the pages that belong to a specific section", "RequiresInputComboBox1": "true", "RequiresInputComboBox2": "true", + "RequiresTwoIds": "false", "RequiresInputTextBox": "false" } ] @@ -191,6 +209,7 @@ "Description": "This is one example to demonstrate how the $expand query parameter can be used to return all notebooks and their descendant sections, sectionGroups in one roundtrip. In this case we are going to make a GET request on ~/notebooks?$expand=sections,sectionGroups($expand=sections)", "RequiresInputComboBox1": "false", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "false" }, { @@ -200,6 +219,7 @@ "Description": "Queries for all notebooks in OneNote and returns a list", "RequiresInputComboBox1": "false", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "false" }, { @@ -209,6 +229,7 @@ "Description": "Following the previous example, first we get a list of all notebooks and then query the metadata for one of the selected notebooks. Please select a specific notebook from the drop-down below", "RequiresInputComboBox1": "true", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "false" }, { @@ -218,6 +239,7 @@ "Description": "Queries for all sections under all notebooks in OneNote and returns a list", "RequiresInputComboBox1": "false", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "false" }, { @@ -227,6 +249,7 @@ "Description": "Following the previous example, first we get a list of all sections and then query the metadata for one of the selected sections. Please select a specific section from the drop-down below", "RequiresInputComboBox1": "true", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "false" }, { @@ -236,6 +259,7 @@ "Description": "Queries for all sectionGroups under all notebooks in OneNote and returns a list. SectionGroups are not very common, so you may see 0 results", "RequiresInputComboBox1": "false", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "false" }, { @@ -245,6 +269,7 @@ "Description": "Query for a list of all notebooks with a given name. This example shows how to use the '$filter=name eq '' query parameter. Please enter the case-sensitive full notebook name to look for in the TextBox below", "RequiresInputComboBox1": "false", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "true" }, { @@ -254,6 +279,7 @@ "Description": "Query for a list of all notebooks where the current user is not an Owner (i.e. the notebooks are owned by someone else and were shared with the current user with either a Reader or Contributor access level). This example shows how to use the '$filter=userRole ne Microsoft.OneNote.Api.UserRole'Owner'' query parameter and will return non-zero results only if the current user has access to shared notebooks", "RequiresInputComboBox1": "false", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "false" }, { @@ -263,6 +289,7 @@ "Description": "Get a sorted list of notebooks using the $orderBy=name asc query parameter and only specific metadata in the response using the $select=id,name query parameter. $orderBy can be used to specify your custom sort order", "RequiresInputComboBox1": "false", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "false" }, { @@ -272,6 +299,7 @@ "Description": "First we get a list of all notebooks and then query for a list of all sections under one of the selected notebooks. Please select a specific notebook from the drop-down below", "RequiresInputComboBox1": "true", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "false" }, { @@ -281,6 +309,7 @@ "Description": "Query for a list of all sections with a given name. This example shows how to use the '$filter=name eq '' query parameter. Please enter the case-sensitive full section name to look for in the TextBox below", "RequiresInputComboBox1": "false", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "true" }, { @@ -290,6 +319,7 @@ "Description": "Create a new blank notebook. Please enter the new notebook's name in the TextBox below", "RequiresInputComboBox1": "false", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "true" }, { @@ -299,6 +329,7 @@ "Description": "First we get a list of all notebooks and then create a new empty section under the selected notebook. Please select a specific notebook from the drop-down below and also enter the new section's name in the TextBox below", "RequiresInputComboBox1": "true", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "true" }, { @@ -309,6 +340,7 @@ "Description": "First we get a list of all section groups and then create a new empty section under the selected section group. Please select a specific section group from the drop-down below and also enter the new section's name in the TextBox below", "RequiresInputComboBox1": "true", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "true" }, { @@ -319,6 +351,7 @@ "Description": "First we get a list of all notebooks and then create a new empty section group under the selected notebook. Please select a specific notebook from the drop-down below and also enter the new section group's name in the TextBox below", "RequiresInputComboBox1": "true", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "true" }, { @@ -329,6 +362,7 @@ "Description": "First we get a list of all section groups and then create a new empty section group under the selected section group. Please select a specific section group from the drop-down below and also enter the new section group's name in the TextBox below", "RequiresInputComboBox1": "true", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "true" } ] @@ -346,6 +380,7 @@ "Description": "Add new trailing content to the default outline in an existing page", "RequiresInputComboBox1": "true", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "false" } ] @@ -363,6 +398,7 @@ "Description": "Delete an existing page", "RequiresInputComboBox1": "true", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "false" } ] @@ -380,6 +416,7 @@ "Description": "Choose the notebook you want to copy, and then enter a name for the new notebook.", "RequiresInputComboBox1": "true", "RequiresInputComboBox2": "false", + "RequiresTwoIds": "false", "RequiresInputTextBox": "true" }, { @@ -389,6 +426,7 @@ "Description": "Choose the section you want to copy and the destination notebook. Then, enter a name for the new section.", "RequiresInputComboBox1": "true", "RequiresInputComboBox2": "true", + "RequiresTwoIds": "true", "RequiresInputTextBox": "true" }, { @@ -398,6 +436,7 @@ "Description": "Choose the section you want to copy and the destination section group. Then, enter a name for the new section.", "RequiresInputComboBox1": "true", "RequiresInputComboBox2": "true", + "RequiresTwoIds": "true", "RequiresInputTextBox": "true" }, { @@ -407,6 +446,7 @@ "Description": "Choose the page you want to copy and the destination section. Then, enter a name for the new page.", "RequiresInputComboBox1": "true", "RequiresInputComboBox2": "true", + "RequiresTwoIds": "true", "RequiresInputTextBox": "true" } ] diff --git a/OneNoteServiceSamplesWinUniversal.Shared/DataModel/SampleDataSource.cs b/OneNoteServiceSamplesWinUniversal.Shared/DataModel/SampleDataSource.cs index 00e3ce1..2760a34 100644 --- a/OneNoteServiceSamplesWinUniversal.Shared/DataModel/SampleDataSource.cs +++ b/OneNoteServiceSamplesWinUniversal.Shared/DataModel/SampleDataSource.cs @@ -29,7 +29,7 @@ namespace OneNoteServiceSamplesWinUniversal.Data public class SampleDataItem { public SampleDataItem(String uniqueId, String title, String subtitle, String imagePath, String description, - bool requiresInputComboBox1, bool requiresInputComboBox2, bool requiresInputTextBox) + bool requiresInputComboBox1, bool requiresInputComboBox2, bool requiresTwoIds, bool requiresInputTextBox) { this.UniqueId = uniqueId; this.Title = title; @@ -38,6 +38,7 @@ public SampleDataItem(String uniqueId, String title, String subtitle, String ima this.ImagePath = imagePath; this.RequiresInputComboBox1 = requiresInputComboBox1; this.RequiresInputComboBox2 = requiresInputComboBox2; + this.RequiresTwoIds = requiresTwoIds; this.RequiresInputTextBox = requiresInputTextBox; } @@ -48,7 +49,8 @@ public SampleDataItem(String uniqueId, String title, String subtitle, String ima public string ImagePath { get; private set; } public bool RequiresInputComboBox1 { get; private set; } - public bool RequiresInputComboBox2 { get; private set; } + public bool RequiresInputComboBox2 { get; private set; } + public bool RequiresTwoIds { get; private set; } public bool RequiresInputTextBox { get; private set; } public override string ToString() @@ -138,6 +140,15 @@ public static async Task GetItemAsync(string uniqueId) return null; } + + private static object CombineLists(List firstList, List secondList) + { + List> combinedLists = new List>(); + combinedLists.Add(firstList); + combinedLists.Add(secondList); + return combinedLists; + } + private async Task GetSampleDataAsync() { if (this._groups.Count != 0) @@ -171,13 +182,14 @@ private async Task GetSampleDataAsync() itemObject["Description"].GetString(), Convert.ToBoolean(itemObject["RequiresInputComboBox1"].GetString()), Convert.ToBoolean(itemObject["RequiresInputComboBox2"].GetString()), + Convert.ToBoolean(itemObject["RequiresTwoIds"].GetString()), Convert.ToBoolean(itemObject["RequiresInputTextBox"].GetString()))); } this.Groups.Add(group); } } - public static async Task ExecuteApi(string uniqueId, bool debug, string requiredSelectedId, + public static async Task ExecuteApi(string uniqueId, bool debug, string requiredSelectedId, string requiredSelectedId2, string requiredInputText, AuthProvider provider, bool useBeta) { var apiEndPoint = ApiEndPoint(useBeta); @@ -258,11 +270,11 @@ public static async Task ExecuteApi(string uniqueId, bool debug, string case "Group-5-Item-0": return await CopyNotebooksExample.CopyNotebook(debug, requiredSelectedId, requiredInputText, provider, apiEndPoint); case "Group-5-Item-1": - return await CopyNotebooksExample.CopyNotebook(debug, requiredSelectedId, requiredInputText, provider, apiEndPoint); + return await CopySectionsExample.CopySectionToNotebook(debug, requiredSelectedId, requiredSelectedId2, requiredInputText, provider, apiEndPoint); case "Group-5-Item-2": - return await CopyNotebooksExample.CopyNotebook(debug, requiredSelectedId, requiredInputText, provider, apiEndPoint); + return await CopySectionsExample.CopySectionToSectionGroup(debug, requiredSelectedId, requiredSelectedId2, requiredInputText, provider, apiEndPoint); case "Group-5-Item-3": - return await CopyNotebooksExample.CopyNotebook(debug, requiredSelectedId, requiredInputText, provider, apiEndPoint); + return await CopyPagesExample.CopyPageToSection(debug, requiredSelectedId, requiredSelectedId2, requiredInputText, provider, apiEndPoint); } return null; } @@ -310,11 +322,17 @@ public static async Task ExecuteApiPrereq(string uniqueId, AuthProvider case "Group-5-Item-0": return await GetNotebooksExample.GetAllNotebooks(false, provider, apiEndPoint); case "Group-5-Item-1": - return await GetNotebooksExample.GetAllNotebooks(false, provider, apiEndPoint); + var sections = await GetSectionsExample.GetAllSections(false, provider, apiEndPoint); + var notebooks = await GetNotebooksExample.GetAllNotebooks(false, provider, apiEndPoint); + return CombineLists(sections, notebooks); case "Group-5-Item-2": - return await GetSectionGroupsExample.GetAllSectionGroups(false, provider, apiEndPoint); + sections = await GetSectionsExample.GetAllSections(false, provider, apiEndPoint); + var sectiongroups = await GetSectionGroupsExample.GetAllSectionGroups(false, provider, apiEndPoint); + return CombineLists(sections, sectiongroups); case "Group-5-Item-3": - return await GetSectionsExample.GetAllSections(false, provider, apiEndPoint); + var pages = await GetPagesExample.GetAllPages(false, provider, apiEndPoint); + sections = await GetSectionsExample.GetAllSections(false, provider, apiEndPoint); + return CombineLists(pages, sections); } return null; } diff --git a/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Notebooks/CopyNotebooksExample.cs b/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Notebooks/CopyNotebooksExample.cs index 85435b6..55981d1 100644 --- a/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Notebooks/CopyNotebooksExample.cs +++ b/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Notebooks/CopyNotebooksExample.cs @@ -27,11 +27,12 @@ namespace OneNoteServiceSamplesWinUniversal.OneNoteApi.Notebooks { /// - /// Class to show an example of copying notebooks via HTTP POST to the CopyNotebook action. - /// If successful, returns a 202 Accepted status code and a Microsoft.OneNote.Api.CopyStatusModel object. + /// Class to show an example of copying notebooks via HTTP POST to the CopyNotebook action. + /// If successful, returns a 202 Accepted status code, a Location header with the notebook's endpoint URL, and a Microsoft.OneNote.Api.CopyStatusModel object. /// /// - /// NOTE: This action requires the ID of the notebook that you want to copy. + /// NOTE: This action requires the ID of the notebook that you want to copy. + /// NOTE: These examples do not include copying to a SharePoint site or group. /// NOTE: It is not the goal of this code sample to produce well re-factored, elegant code. /// You may notice code blocks being duplicated in various places in this project. /// We have deliberately added these code blocks to allow anyone browsing the sample @@ -56,10 +57,10 @@ public static class CopyNotebooksExample /// /// Run the code under the debugger /// ID of the notebook to copy - /// Name for the new notebook + /// Name for the new notebook. Must be unique in the destination. /// Live Connect or Azure AD /// v1.0 or beta path - /// If you don't use the renameAs parameter, the notebook is overridden. + /// /// The converted HTTP response message public static async Task CopyNotebook(bool debug, string notebookId, string newNotebookName, AuthProvider provider, string apiRoute) { @@ -80,10 +81,11 @@ public static async Task CopyNotebook(bool debug, string notebo // Prepare an HTTP POST request to the CopyNotebook endpoint of the target notebook. // The request body content type is application/json. - // *Optional* parameters are: - // - siteCollectionId and siteId: The SharePoint site to copy the notebook to (if you're copying to a site). - // - groupId: The ID of the group to copy to the notebook to (if you're copying to a group). - // - renameAs: The name for the copy of the notebook. Overwrites the target notebook if not provided. + // Request body parameters: + // - siteCollectionId and siteId: The SharePoint site to copy the notebook to. Required to copy to a site. + // - groupId: The ID of the group to copy to the notebook to. Required to copy to a group. + // - renameAs: The name for the copy of the notebook. Returns 409 (Conflict) if a notebook with the same name exists + // in the destination location. If omitted, defaults to the name of the existing notebook. var createMessage = new HttpRequestMessage(HttpMethod.Post, apiRoute + "notebooks/" + notebookId + "/Microsoft.OneNote.Api.CopyNotebook") { Content = new StringContent("{ renameAs : '" + newNotebookName + "' }", Encoding.UTF8, "application/json") diff --git a/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Pages/CopyPagesExample.cs b/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Pages/CopyPagesExample.cs index a7a30da..aacb0ea 100644 --- a/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Pages/CopyPagesExample.cs +++ b/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Pages/CopyPagesExample.cs @@ -28,10 +28,11 @@ namespace OneNoteServiceSamplesWinUniversal.OneNoteApi.Pages { /// /// Class to show an example of copying notebooks via HTTP POST to the CopyToSection action. - /// If successful, returns a 202 Accepted status code and a Microsoft.OneNote.Api.CopyStatusModel object. + /// If successful, returns a 202 Accepted status code, a Location header with the page's endpoint URL, and a Microsoft.OneNote.Api.CopyStatusModel object. /// /// - /// NOTE: This action requires the ID of the page that you want to copy and the ID of the section you want to copy the page to. + /// NOTE: This action requires the ID of the page that you want to copy and the ID of the section you want to copy the page to. + /// NOTE: These examples do not include copying to a SharePoint site or group. /// NOTE: It is not the goal of this code sample to produce well re-factored, elegant code. /// You may notice code blocks being duplicated in various places in this project. /// We have deliberately added these code blocks to allow anyone browsing the sample @@ -43,7 +44,7 @@ namespace OneNoteServiceSamplesWinUniversal.OneNoteApi.Pages /// client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await Auth.GetAuthToken()); /// var createMessage = new HttpRequestMessage(HttpMethod.Post, "https://www.onenote.com/api/beta/me/notes/pages/{id}/Microsoft.OneNote.Api.CopyToSection") /// { - /// Content = new StringContent("{"{ id : 'page-id', renameAs: 'New Page Name' }", System.Text.Encoding.UTF8, "application/json") + /// Content = new StringContent("{ id : 'page-id', renameAs: 'New Page Name' }", System.Text.Encoding.UTF8, "application/json") /// }; /// HttpResponseMessage response = await client.SendAsync(createMessage); /// @@ -52,7 +53,7 @@ public static class CopyPagesExample #region Examples of POST https://www.onenote.com/api/beta/me/notes/pages/{id}/Microsoft.OneNote.Api.CopyToSection /// - /// Copy a page + /// Copy a page to a section /// /// Run the code under the debugger /// ID of the page to copy @@ -60,7 +61,7 @@ public static class CopyPagesExample /// Name for the new page /// Live Connect or Azure AD /// v1.0 or beta path - /// If you don't use the renameAs parameter, the page is overridden. + /// /// The converted HTTP response message public static async Task CopyPageToSection(bool debug, string pageId, string sectionId, string newPageName, AuthProvider provider, string apiRoute) { @@ -79,12 +80,13 @@ public static async Task CopyPageToSection(bool debug, string p client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await Auth.GetAuthToken(provider)); - // Prepare an HTTP POST request to the CopyToSection endpoint of the target notebook. + // Prepare an HTTP POST request to the CopyToSection endpoint of the target page. // The request body content type is application/json. - // Requires the ID of the section to copy to. *Optional* parameters are: - // - siteCollectionId and siteId: The SharePoint site to copy the notebook to (if you're copying to a site). - // - groupId: The ID of the group to copy to the notebook to (if you're copying to a group). - // - renameAs: The name for the copy of the notebook. Overwrites the target notebook if not provided. + // Request body parameters: + // - id: The ID of the section to copy to. Required for all CopyToSection calls. + // - siteCollectionId and siteId: The SharePoint site to copy the page to. Required to copy to a site. + // - groupId: The ID of the group to copy to the page to. Required to copy to a group. + // - renameAs: The name for the copy of the page. If omitted, defaults to the name of the existing page. //xx var createMessage = new HttpRequestMessage(HttpMethod.Post, apiRoute + "pages/" + pageId + "/Microsoft.OneNote.Api.CopyToSection") { Content = new StringContent("{ id : '" + sectionId + "', renameAs : '" + newPageName + "' }", Encoding.UTF8, "application/json") diff --git a/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Sections/CopySectionsExample.cs b/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Sections/CopySectionsExample.cs index fbc3943..3ea5cbc 100644 --- a/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Sections/CopySectionsExample.cs +++ b/OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Sections/CopySectionsExample.cs @@ -28,10 +28,12 @@ namespace OneNoteServiceSamplesWinUniversal.OneNoteApi.Sections { /// /// Class to show examples of copying sections via HTTP POST to the CopyToNotebook and CopyToSectionGroup actions. - /// If successful, returns a 202 Accepted status code and a Microsoft.OneNote.Api.CopyStatusModel object. + /// If successful, returns a 202 Accepted status code, a Location header with the section's endpoint URL, and a Microsoft.OneNote.Api.CopyStatusModel object. /// /// - /// NOTE: This action requires the ID of the section that you want to copy and the ID of the notebook or sectiongroup that you want to copy to. + /// NOTE: These actions require the ID of the section that you want to copy and the ID of the notebook or section group + /// that you want to copy the section to. + /// NOTE: These examples do not include copying to a SharePoint site or group. /// NOTE: It is not the goal of this code sample to produce well re-factored, elegant code. /// You may notice code blocks being duplicated in various places in this project. /// We have deliberately added these code blocks to allow anyone browsing the sample @@ -43,7 +45,7 @@ namespace OneNoteServiceSamplesWinUniversal.OneNoteApi.Sections /// client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await Auth.GetAuthToken()); /// var createMessage = new HttpRequestMessage(HttpMethod.Post, "https://www.onenote.com/api/beta/me/notes/sections/{id}/Microsoft.OneNote.Api.CopyToNotebook") /// { - /// Content = new StringContent("{ renameAs: 'New Section Name' }", System.Text.Encoding.UTF8, "application/json") + /// Content = new StringContent("{ id : 'section-id', renameAs: 'New Section Name' }", System.Text.Encoding.UTF8, "application/json") /// }; /// HttpResponseMessage response = await client.SendAsync(createMessage); /// @@ -52,15 +54,15 @@ public static class CopySectionsExample #region Examples of POST https://www.onenote.com/api/beta/me/notes/sections/{id}/Microsoft.OneNote.Api.CopyToNotebook /// - /// Copy a section + /// Copy a section to a notebook /// /// Run the code under the debugger /// Id of the section to copy /// Id of the destination notebook - /// Name for the new section + /// Name for the new section. Must be unique in the destination notebook. /// Live Connect or Azure AD /// v1.0 or beta path - /// If you don't use the renameAs parameter, the section is overridden. + /// /// The converted HTTP response message public static async Task CopySectionToNotebook(bool debug, string sectionId, string notebookId, string newSectionName, AuthProvider provider, string apiRoute) { @@ -81,13 +83,15 @@ public static async Task CopySectionToNotebook(bool debug, stri // Prepare an HTTP POST request to the CopyToNotebook endpoint of the target section. // The request body content type is application/json. - // Requires the ID of the notebook to copy to. Optional parameters are: - // - siteCollectionId and siteId: The SharePoint site to copy the section to (if you're copying to a site). - // - groupId: The ID of the group to copy to the section to (if you're copying to a group). - // - renameAs: The name for the copy of the section. Overwrites the target section if not provided. + // Request body parameters: + // - id: The ID of the notebook to copy to. Required for all CopyToNotebook calls. + // - siteCollectionId and siteId: The SharePoint site to copy the section to. Required to copy to a site. + // - groupId: The ID of the group to copy the section to. Required to copy to a group. + // - renameAs: The name for the copy of the section. Returns 409 (Conflict) if a section with the same name exists + // in the destination notebook. If omitted, defaults to the name of the existing section. var createMessage = new HttpRequestMessage(HttpMethod.Post, apiRoute + "sections/" + sectionId + "/Microsoft.OneNote.Api.CopyToNotebook") { - Content = new StringContent("{ renameAs : '" + newSectionName + "' }", Encoding.UTF8, "application/json") + Content = new StringContent("{ id : '" + notebookId + "', renameAs : '" + newSectionName + "' }", Encoding.UTF8, "application/json") }; HttpResponseMessage response = await client.SendAsync(createMessage); @@ -104,10 +108,10 @@ public static async Task CopySectionToNotebook(bool debug, stri /// Run the code under the debugger /// Id of the section to copy /// Id of the destination section group - /// Name for the new section + /// Name for the new section. Must be unique in the destination section group. /// Live Connect or Azure AD /// v1.0 or beta path - /// If you don't use the renameAs parameter, the section is overridden. + /// /// The converted HTTP response message public static async Task CopySectionToSectionGroup(bool debug, string sectionId, string sectionGroupId, string newSectionName, AuthProvider provider, string apiRoute) { @@ -126,15 +130,17 @@ public static async Task CopySectionToSectionGroup(bool debug, client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await Auth.GetAuthToken(provider)); - // Prepare an HTTP POST request to the CopyToNotebook endpoint of the target section. + // Prepare an HTTP POST request to the CopyToSectionGroup endpoint of the target section. // The request body content type is application/json. - // Requires the ID of the section group to copy to. Optional parameters are: - // - siteCollectionId and siteId: The SharePoint site to copy the section to (if you're copying to a site). - // - groupId: The ID of the group to copy to the section to (if you're copying to a group). - // - renameAs: The name for the copy of the section. Overwrites the target section if not provided. + // Request body parameters: + // - id: The ID of the section group to copy to. Required for all CopyToSectionGroup calls. + // - siteCollectionId and siteId: The SharePoint site to copy the section to. Required to copy to a site. + // - groupId: The ID of the group to copy to the section to. Required to copy to a group. + // - renameAs: The name for the copy of the section. Returns 409 (Conflict) if a section with the same name exists + // in the destination section group. If omitted, defaults to the name of the existing section. var createMessage = new HttpRequestMessage(HttpMethod.Post, apiRoute + "sections/" + sectionId + "/Microsoft.OneNote.Api.CopyToSectionGroup") { - Content = new StringContent("{ renameAs : '" + newSectionName + "' }", Encoding.UTF8, "application/json") + Content = new StringContent("{ id : '" + sectionGroupId + "', renameAs : '" + newSectionName + "' }", Encoding.UTF8, "application/json") }; HttpResponseMessage response = await client.SendAsync(createMessage); diff --git a/OneNoteServiceSamplesWinUniversal.Windows/ItemPage.xaml.cs b/OneNoteServiceSamplesWinUniversal.Windows/ItemPage.xaml.cs index a010f10..3400c0b 100644 --- a/OneNoteServiceSamplesWinUniversal.Windows/ItemPage.xaml.cs +++ b/OneNoteServiceSamplesWinUniversal.Windows/ItemPage.xaml.cs @@ -79,7 +79,7 @@ private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs Model.Item = item; InputSelectionPanel2.Visibility = (item.RequiresInputComboBox2) ? Visibility.Visible : Visibility.Collapsed; InputTextBox.Visibility = (item.RequiresInputTextBox) ? Visibility.Visible : Visibility.Collapsed; - if (item.RequiresInputComboBox1) + if ((item.RequiresInputComboBox1) && (!item.RequiresTwoIds)) { var response = await SampleDataSource.ExecuteApiPrereq(item.UniqueId, UserData.Provider, UserData.UseBeta); if (response is List) @@ -88,6 +88,17 @@ private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs InputSelectionPanel1.Visibility = Visibility.Visible; } } + if (item.RequiresTwoIds) + { + var response = await SampleDataSource.ExecuteApiPrereq(item.UniqueId, UserData.Provider, UserData.UseBeta); + if (response is List>) + { + List> lists = (List>) response; + InputComboBox1.ItemsSource = lists.ElementAt(0); + InputComboBox2.ItemsSource = lists.ElementAt(1); + InputSelectionPanel1.Visibility = Visibility.Visible; + } + } else { InputSelectionPanel1.Visibility = Visibility.Collapsed; @@ -136,7 +147,8 @@ private async Task ExecuteApiAction(bool debug, SampleDataItem item) Model.ApiResponse = null; string requiredSelectedId = null; - if (item.RequiresInputComboBox1) + string requiredSelectedId2= null; + if ((item.RequiresInputComboBox1) && (!item.RequiresTwoIds)) { var selectedItem = (ApiBaseResponse) InputComboBox1.SelectedItem; if (selectedItem != null) @@ -149,7 +161,7 @@ private async Task ExecuteApiAction(bool debug, SampleDataItem item) return; } } - if (item.RequiresInputComboBox2) + if ((item.RequiresInputComboBox2) && (!item.RequiresTwoIds)) { var selectedItem = (ApiBaseResponse) InputComboBox2.SelectedItem; if (selectedItem != null) @@ -162,7 +174,24 @@ private async Task ExecuteApiAction(bool debug, SampleDataItem item) return; } } - + + // The operation requires two IDs (such as the Copy actions). + if (item.RequiresTwoIds) + { + var selectedItem1 = (ApiBaseResponse)InputComboBox1.SelectedItem; + var selectedItem2 = (ApiBaseResponse)InputComboBox2.SelectedItem; + if ((selectedItem1 != null) && (selectedItem2 != null)) + { + requiredSelectedId = selectedItem1.Id; + requiredSelectedId2 = selectedItem2.Id; + } + else + { + FlyoutBase.ShowAttachedFlyout(InputComboBox1); + FlyoutBase.ShowAttachedFlyout(InputComboBox2); + return; + } + } string requiredInputText = null; if (item.RequiresInputTextBox) { @@ -174,7 +203,7 @@ private async Task ExecuteApiAction(bool debug, SampleDataItem item) } } - Model.ApiResponse = await SampleDataSource.ExecuteApi(item.UniqueId, debug, requiredSelectedId, requiredInputText, UserData.Provider, UserData.UseBeta); + Model.ApiResponse = await SampleDataSource.ExecuteApi(item.UniqueId, debug, requiredSelectedId, requiredSelectedId2, requiredInputText, UserData.Provider, UserData.UseBeta); } ///