首页 > 其他专区 > OneNote >

如何创建OneNote的笔记本、章节和页面

OneNote 2022-04-21 22:10:25

本文不会具体介绍OneNote,简单地概括来说,OneNote越用越爽越停不下来,功能很强大,体验很好。下面的图示为了说明笔记本(Notebook)、章节(Section)和页面(Page)的关系。

如何创建OneNote的笔记本、章节和页面
 

我们可以在各种资源上创建创建OneNote的笔记本,包括个人的终结点、SharePoint Online工作组网站、SharePoint Online个人网站和Office 365组网站。分别对应如下终结点:

/me/onenote/notebooks
/sites/{id}/onenote/notebooks
/users/{id | userPrincipalName}/onenote/notebooks
/groups/{id | userPrincipalName}/onenote/notebooks
本文我们将演示如何为给定用户的个人网站添加OneNote的笔记本、章节和页面。了解了它之后,向其他位置添加也是类似的,仅需要很小的改动。

需要的权限
OneNote的笔记本可以以多种权限创建。我们为了方便演示会直接添加Notes.ReadWrite.All这个应用程序权限,这个权限可以让我们为其他用户创建和更新notebooks。当然我们还可以使用托管权限Notes.Create或Notes.ReadWrite去实现类似的操作。

创建OneNote笔记本
为给定用户创建记事本,我们需要传入用户的账号和笔记本的名字,然后访问OneNote终结点进行创建。
public async Task CreateNoteBook(string upn, string notebookName)
{
var notebook = new Notebook
{
DisplayName = notebookName
};
return (await _graphClient.Users[upn].Onenote.Notebooks.Request().AddAsync(notebook));
}
 

创建OneNote章节
创建笔记本之后,我们在这个笔记本的基础上创建章节,因此需要引用笔记本对象Notebook。

public async Task CreateSection(string upn, Notebook notebook, string sectionName)
{
var section = new OnenoteSection
{
DisplayName = sectionName
};
return (await _graphClient.Users[upn].Onenote.Notebooks[notebook.Id].Sections.Request().AddAsync(section));
}
创建OneNote页面
最后一步就是在章节中创建页面了。有趣的是这里我们可以从/Users/OneNote/Sections直接引用/Pages终结点,而不需要再经由/Notebooks终结点了。为了更直接的说明,创建页面的代码我们采用HttpClient的方式。

public async Task CreatePage(string upn, OnenoteSection section, string pageName)
{
Uri Uri = new Uri($"https://graph.microsoft.com/v1.0/users/{upn}/onenote/sections/{section.Id}/pages");
// use a verbatim interpolated string to represetnt the HTML text to be used for page creation
var html = $@"



{pageName}


I'm learning about the Microsoft Graph!

";
HttpContent httpContent = new StringContent(html, System.Text.Encoding.UTF8, "application/xhtml+xml");
return (await _httpClient.PostAsync(Uri, httpContent));
}
 

总结
本篇示例中,我们在Helpers文件夹中新建了一个类文件OneNoteHelper.cs,用于执行OneNote相关的操作。并在Program中调用。
执行结果如下:

如何创建OneNote的笔记本、章节和页面1
如何创建OneNote的笔记本、章节和页面2
 

当然,如果你本地的Window 10也装了OneNote的话,体验一下什么是移动为先,云为先。

如何创建OneNote的笔记本、章节和页面
 


示例代码已更新,戳这里。

如何创建OneNote的笔记本、章节和页面的下载地址:
  • 本地下载

  • 标签: 如何创建oneNote笔记本章节页面

    office教程网 Copyright © 2016-2020 http://www.office9.cn. Some Rights Reserved. 苏ICP备20040415号-9