Skip to content
This repository was archived by the owner on Jun 17, 2024. It is now read-only.

Commit 3213ec2

Browse files
committed
Merge pull request #3 from OneNoteDev/copyFunctions
Add API sample code for Create section in notebook/section group and Create section groups. Promote some existing Beta Samples to Production
2 parents 9bfc248 + 05545f6 commit 3213ec2

File tree

7 files changed

+286
-49
lines changed

7 files changed

+286
-49
lines changed

OneNoteServiceSamplesWinUniversal.Shared/DataModel/SampleData.json

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,39 @@
294294
},
295295
{
296296
"UniqueId": "Group-2-Item-12",
297-
"Title": "Create a new section",
297+
"Title": "Create a new section in a notebook",
298298
"ImagePath": "Assets/MediumGray.png",
299-
"Description": "First we get a list of all notebooks and then create a new blank section under one of the selected notebooks. Please select a specific notebook from the drop-down below and also enter the new section's name in the TextBox below",
299+
"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",
300+
"RequiresInputComboBox1": "true",
301+
"RequiresInputComboBox2": "false",
302+
"RequiresInputTextBox": "true"
303+
},
304+
{
305+
"UniqueId": "Group-2-Item-13",
306+
"Title": "Create a new section in a section group",
307+
"Subtitle": "BETA Only",
308+
"ImagePath": "Assets/MediumGray.png",
309+
"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",
310+
"RequiresInputComboBox1": "true",
311+
"RequiresInputComboBox2": "false",
312+
"RequiresInputTextBox": "true"
313+
},
314+
{
315+
"UniqueId": "Group-2-Item-14",
316+
"Title": "Create a new section group in a notebook",
317+
"Subtitle": "BETA Only",
318+
"ImagePath": "Assets/MediumGray.png",
319+
"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",
320+
"RequiresInputComboBox1": "true",
321+
"RequiresInputComboBox2": "false",
322+
"RequiresInputTextBox": "true"
323+
},
324+
{
325+
"UniqueId": "Group-2-Item-15",
326+
"Title": "Create a new section group in a section group",
327+
"Subtitle": "BETA Only",
328+
"ImagePath": "Assets/MediumGray.png",
329+
"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",
300330
"RequiresInputComboBox1": "true",
301331
"RequiresInputComboBox2": "false",
302332
"RequiresInputTextBox": "true"
@@ -312,7 +342,6 @@
312342
{
313343
"UniqueId": "Group-3-Item-0",
314344
"Title": "Append to the end of the default outline in the page",
315-
"Subtitle": "BETA Only",
316345
"ImagePath": "Assets/MediumGray.png",
317346
"Description": "Add new trailing content to the default outline in an existing page",
318347
"RequiresInputComboBox1": "true",
@@ -330,7 +359,6 @@
330359
{
331360
"UniqueId": "Group-4-Item-0",
332361
"Title": "Delete Page",
333-
"Subtitle": "BETA Only",
334362
"ImagePath": "Assets/LightGray.png",
335363
"Description": "Delete an existing page",
336364
"RequiresInputComboBox1": "true",

OneNoteServiceSamplesWinUniversal.Shared/DataModel/SampleDataSource.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,13 @@ public static async Task<object> ExecuteApi(string uniqueId, bool debug, string
244244
case "Group-2-Item-11":
245245
return await PostNotebooksExample.CreateSimpleNotebook(debug, requiredInputText, provider, apiEndPoint);
246246
case "Group-2-Item-12":
247-
return await PostSectionsExample.CreateSimpleSection(debug, requiredSelectedId, requiredInputText, provider, apiEndPoint);
247+
return await PostSectionsExample.CreateSectionInNotebook(debug, requiredSelectedId, requiredInputText, provider, apiEndPoint);
248+
case "Group-2-Item-13":
249+
return await PostSectionsExample.CreateSectionInSectionGroup(debug, requiredSelectedId, requiredInputText, provider, apiEndPoint);
250+
case "Group-2-Item-14":
251+
return await PostSectionGroupsExample.CreateSectionGroupInNotebook(debug, requiredSelectedId, requiredInputText, provider, apiEndPoint);
252+
case "Group-2-Item-15":
253+
return await PostSectionGroupsExample.CreateSectionGroupInSectionGroup(debug, requiredSelectedId, requiredInputText, provider, apiEndPoint);
248254
case "Group-3-Item-0":
249255
return await PatchPagesExample.AppendToDefaultOutlineInPageContent(debug, requiredSelectedId, provider, apiEndPoint);
250256
case "Group-4-Item-0":
@@ -281,7 +287,13 @@ public static async Task<object> ExecuteApiPrereq(string uniqueId, AuthProvider
281287
case "Group-2-Item-9":
282288
return await GetNotebooksExample.GetAllNotebooks(false, provider, apiEndPoint);
283289
case "Group-2-Item-12":
284-
return await GetNotebooksExample.GetAllNotebooks(false, provider, apiEndPoint);
290+
return await GetNotebooksExample.GetAllNotebooks(false, provider, apiEndPoint);
291+
case "Group-2-Item-13":
292+
return await GetSectionGroupsExample.GetAllSectionGroups(false, provider, apiEndPoint);
293+
case "Group-2-Item-14":
294+
return await GetNotebooksExample.GetAllNotebooks(false, provider, apiEndPoint);
295+
case "Group-2-Item-15":
296+
return await GetSectionGroupsExample.GetAllSectionGroups(false, provider, apiEndPoint);
285297
case "Group-3-Item-0":
286298
return await GetPagesExample.GetAllPages(false, provider, apiEndPoint);
287299
case "Group-3-Item-1":

OneNoteServiceSamplesWinUniversal.Shared/OneNoteApi/Pages/DeletePageExample.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
namespace OneNoteServiceSamplesWinUniversal.OneNoteApi.Pages
2626
{
2727
/// <summary>
28-
/// Class to show a selection of examples creating pages via HTTP DELETE to the OneNote API
28+
/// Class to show a selection of examples deleting pages via HTTP DELETE to the OneNote API
2929
/// - Delete a new page is represented via the DELETE HTTP verb.
30-
/// For more info, see http://dev.onenote.com/docs
30+
/// For more info, see http://dev.onenote.com/docs#/reference/delete-pages.
3131
/// </summary>
3232
/// <remarks>
3333
/// NOTE: It is not the goal of this code sample to produce well re-factored, elegant code.
@@ -39,7 +39,7 @@ namespace OneNoteServiceSamplesWinUniversal.OneNoteApi.Pages
3939
/// var client = new HttpClient();
4040
/// client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await Auth.GetAuthToken());
4141
///
42-
/// var deleteMessage = new HttpRequestMessage(HttpMethod.Delete, "https://www.onenote.com/beta/v1.0/pages");
42+
/// var deleteMessage = new HttpRequestMessage(HttpMethod.Delete, "https://www.onenote.com/api/v1.0/pages");
4343
/// HttpResponseMessage response = await client.SendAsync(deleteMessage);
4444
/// </code>
4545
public static class DeletePagesExample
@@ -53,7 +53,7 @@ public static class DeletePagesExample
5353
/// <param name="pageId"></param>
5454
/// <param name="provider"></param>
5555
/// <param name="apiRoute"></param>
56-
/// <remarks>Create page using a single part text/html content type</remarks>
56+
/// <remarks>Delete a specified page by ID.</remarks>
5757
/// <returns>The converted HTTP response message</returns>
5858
public static async Task<ApiBaseResponse> DeletePage(bool debug, string pageId, AuthProvider provider, string apiRoute)
5959
{
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
//*********************************************************
2+
// Copyright (c) Microsoft Corporation
3+
// All rights reserved.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the ""License"");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT
11+
// WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS
12+
// OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
13+
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR
14+
// PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
15+
//
16+
// See the Apache Version 2.0 License for specific language
17+
// governing permissions and limitations under the License.
18+
//*********************************************************
19+
20+
using Newtonsoft.Json;
21+
using System.Collections.Generic;
22+
using System.Diagnostics;
23+
using System.Linq;
24+
using System.Net;
25+
using System.Net.Http;
26+
using System.Net.Http.Headers;
27+
using System.Text;
28+
using System.Threading.Tasks;
29+
30+
namespace OneNoteServiceSamplesWinUniversal.OneNoteApi.SectionGroups
31+
{
32+
/// <summary>
33+
/// Class to show a selection of examples creating section groups via HTTP POST to the OneNote API
34+
/// - Creating a new section group is represented via the POST HTTP verb.
35+
/// - Creating a new section group under a given notebook is represented by the Uri: https://www.onenote.com/api/beta/me/notes/notebooks/{notebookId}/sectiongroups
36+
/// - Creating a new section group under a given section group is represented by the Uri: https://www.onenote.com/api/beta/me/notes/sectiongroups/{sectionGroupId}/sectiongroups
37+
/// For more info, see http://dev.onenote.com/docs
38+
/// </summary>
39+
/// <remarks>
40+
/// NOTE: All create-sectiongroups operations require a parent notebook or parent sectiongroup.
41+
/// The section group name is specified in the request body.
42+
/// NOTE: It is not the goal of this code sample to produce well re-factored, elegant code.
43+
/// You may notice code blocks being duplicated in various places in this project.
44+
/// We have deliberately added these code blocks to allow anyone browsing the sample
45+
/// to easily view all related functionality in near proximity.
46+
/// </remarks>
47+
/// <code>
48+
/// var client = new HttpClient();
49+
/// client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
50+
/// client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await Auth.GetAuthToken());
51+
/// var createMessage = new HttpRequestMessage(HttpMethod.Post, "https://www.onenote.com/api/v1.0/me/notes/notebooks/{notebookId}/sectiongroups")
52+
/// {
53+
/// Content = new StringContent("{name: NewSectionGroupName }", System.Text.Encoding.UTF8, "application/json")
54+
/// };
55+
/// HttpResponseMessage response = await client.SendAsync(createMessage);
56+
/// </code>
57+
public static class PostSectionGroupsExample
58+
{
59+
#region Examples of POST https://www.onenote.com/api/beta/me/notes/notebooks/{notebookId}/sectiongroups
60+
61+
/// <summary>
62+
/// BETA Create a section group with a given name under a given notebookId
63+
/// </summary>
64+
/// <param name="debug">Run the code under the debugger</param>
65+
/// <param name="notebookId">parent notebook's Id</param>
66+
/// <param name="sectionName">name of the section group to create</param>
67+
/// <param name="provider"></param>
68+
/// <param name="apiRoute"></param>
69+
/// <remarks>Create section group using a application/json content type</remarks>
70+
/// <returns>The converted HTTP response message</returns>
71+
public static async Task<ApiBaseResponse> CreateSectionGroupInNotebook(bool debug, string notebookId, string sectionGroupName, AuthProvider provider, string apiRoute)
72+
{
73+
if (debug)
74+
{
75+
Debugger.Launch();
76+
Debugger.Break();
77+
}
78+
79+
var client = new HttpClient();
80+
81+
// Note: API only supports JSON response.
82+
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
83+
84+
// Not adding the Authentication header would produce an unauthorized call and the API will return a 401
85+
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",
86+
await Auth.GetAuthToken(provider));
87+
88+
// Prepare an HTTP POST request to the SectionGroups endpoint
89+
// The request body content type is application/json and requires a name property
90+
var createMessage = new HttpRequestMessage(HttpMethod.Post, apiRoute + @"notebooks/" + notebookId + "/sectiongroups")
91+
{
92+
Content = new StringContent("{ name : '" + WebUtility.UrlEncode(sectionGroupName) + "' }", Encoding.UTF8, "application/json")
93+
};
94+
95+
HttpResponseMessage response = await client.SendAsync(createMessage);
96+
97+
return await HttpUtils.TranslateResponse(response);
98+
}
99+
100+
#endregion
101+
102+
103+
#region Examples of POST https://www.onenote.com/api/beta/me/notes/sectiongroups/{sectionGroupId}/sectiongroups
104+
/// <summary>
105+
/// BETA Create a section group with a given name under a given sectionGroupId
106+
/// </summary>
107+
/// <param name="debug">Run the code under the debugger</param>
108+
/// <param name="notebookId">parent section group's Id</param>
109+
/// <param name="sectionName">name of the section group to create</param>
110+
/// <param name="provider"></param>
111+
/// <param name="apiRoute"></param>
112+
/// <remarks>Create section group using a application/json content type</remarks>
113+
/// <returns>The converted HTTP response message</returns>
114+
public static async Task<ApiBaseResponse> CreateSectionGroupInSectionGroup(bool debug, string sectionGroupId, string sectionGroupName, AuthProvider provider, string apiRoute)
115+
{
116+
if (debug)
117+
{
118+
Debugger.Launch();
119+
Debugger.Break();
120+
}
121+
122+
var client = new HttpClient();
123+
124+
// Note: API only supports JSON response.
125+
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
126+
127+
// Not adding the Authentication header would produce an unauthorized call and the API will return a 401
128+
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",
129+
await Auth.GetAuthToken(provider));
130+
131+
// Prepare an HTTP POST request to the SectionGroups endpoint
132+
// The request body content type is application/json and requires a name property
133+
var createMessage = new HttpRequestMessage(HttpMethod.Post, apiRoute + @"sectiongroups/" + sectionGroupId + "/sectiongroups")
134+
{
135+
Content = new StringContent("{ name : '" + WebUtility.UrlEncode(sectionGroupName) + "' }", Encoding.UTF8, "application/json")
136+
};
137+
138+
HttpResponseMessage response = await client.SendAsync(createMessage);
139+
140+
return await HttpUtils.TranslateResponse(response);
141+
}
142+
143+
#endregion
144+
}
145+
}

0 commit comments

Comments
 (0)