3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Microsoft Graph を使って OneNote の文書の内容を取得する

Last updated at Posted at 2021-04-06

Microsoft Graph を使うと OneNote にアクセスできる。

Microsoft Graph Exploer

を使うと 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)

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 を便利に活用できそうです。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?