Microsoft Graph を使うと OneNote にアクセスできる。
を使うと Microsoft 365 の いろいろな情報にアクセスできるようです。
Outlook の メールや OneNote の ドキュメントの中にまでアクセスできるようです。
https://graph.microsoft.com/v1.0/me/onenote/notebooks
のように URL を使って目的の情報にアクセスしたり、プログラムによって情報取得もできるようです。
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var notebooks = await graphClient.Me.Onenote.Notebooks
.Request()
.GetAsync();
早速 ためしてみましょう。
サンプルプログラムの実行環境
マイクロソフトの クイックスタート のページを参考に
アプリを作成して データ取得してみましたが、
Windows Form 版が無かったので 作成して github に公開しました。
Microsoft Graph API サンプル (Windows Form)
OneNote の 直近の1週間の修正した ドキュメントを取得するコードは 以下のようになります。
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
// Note の セクション一覧を取得
var notebooks = await graphClient.Me.Onenote.Sections
.Request()
.GetAsync();
var sectionIdList = notebooks.Select(r => r.Id);
List<Entity> getDocList = new List<Entity>();
foreach (string secId in sectionIdList)
{
// 指定の セクションのページ一覧を取得
var pageList = await graphClient.Me.Onenote.Sections[secId].Pages.Request()
.GetAsync();
foreach (var page in pageList)
{
// 指定のページの最終更新年月日を比較 新しい物を取得対象とする。
Debug.WriteLine(page.Title + " " + page.LastModifiedDateTime);
Debug.WriteLine(page.Links.OneNoteWebUrl.Href);
Debug.WriteLine(page.ParentSection.Id);
Debug.WriteLine(page.ParentSection.DisplayName);
if (page.LastModifiedDateTime > DateTime.Now.AddDays(-7))
{
getDocList.Add(page);
}
}
}
// 指定の ページを取得する。
foreach (var doc in getDocList)
{
// ページ取得のループ
var contentSt = await graphClient.Me.Onenote.Pages[doc.Id].Content.Request().GetAsync();
var sr = new StreamReader(contentSt);
string s = await sr.ReadToEndAsync();
Debug.WriteLine(s);
}
他にもメール送信したり いろいろ Microsoft 365 を便利に活用できそうです。