ささみ学習帳 - sasami's study book

ささみ学習帳

Microsoft365 や Power Platform について学んだこと・アイデアのメモ

Power Automate クラウドフローでOutlook メールをカテゴリ(分類)を条件に検索する

 

はじめに

Power Automate クラウドフローでOutlook メールをカテゴリ(分類)で検索する方法です。



考え方

Power Automate のOffice 365 Outlook コネクタには「メールを取得する(V3)」アクションでメールを取得することができますが、このアクションはカテゴリ(分類)を条件にメールを取得する機能がありません。また、取得結果の情報にもカテゴリは含まれていない為、後処理で何とかすることもできません。

ですが「HTTP 要求を送信します」アクションを使ってGraph APIを呼び出すことで検索することが可能です。

 

フロー全体図

「オレンジの分類」が付加されたメールを検索するフロー


フロー解説

[Office 365 Outlook]HTTP要求を送信します

 

 

このようなJSONで返されます。「メールを取得する(V3)」アクションでは取得できない多くの情報も得られるのもうれしい点です。

{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('448060bd-fe71-424f-b70b-3456084c7ffd')/messages",
  "value": [
    {
      "@odata.etag": "W/\"CQAAABYAAACVD3WgHj8MTpdR6wepif3oAAC/FlQr\"",
      "id": "AAMkADcyZTA5MTgt---省略---3oAAAAAAEJAACMTpdR6wepif3oAAC_piGIAAA=",
      "createdDateTime": "2024-01-30T12:42:52Z",
      "lastModifiedDateTime": "2024-01-31T13:46:39Z",
      "changeKey": "CQAAABYAAACVD3WgHj8MTpdR6wepif3oAAC/FlQr",
      "categories": [
        "オレンジの分類"
      ],
      "receivedDateTime": "2024-01-30T12:42:57Z",
      "sentDateTime": "2024-01-30T12:42:56Z",
      "hasAttachments": false,
      "internetMessageId": "<MN2P---省略--R06MB6606.namprd06.prod.outlook.com>",
      "subject": "Re: オンラインミーティングソフトウェアの障害について",
      "bodyPreview": "--------省略--------",
      "importance": "normal",
      "parentFolderId": "AAMkADcyZTJiYjIzLTA5MTgt---省略---3oAAAAAAEJAAA=",
      "conversationId": "AAQkADcyZTJiYjIzLTA5---省略---G9Ikx8DbACZKSI=",
      "conversationIndex": "AQHaU---省略---veAAAAZxA==",
      "isDeliveryReceiptRequested": false,
      "isReadReceiptRequested": false,
      "isRead": true,
      "isDraft": false,
      "webLink": "https://outlook.office365.com/owa/?ItemID=AAMkADcy---省略---3D&exvsurl=1&viewmodel=ReadMessageItem",
      "inferenceClassification": "focused",
      "body": {
        "contentType": "html",
        "content": "<html><head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">--------省略--------</html>"
      },
      "sender": {
        "emailAddress": {
          "name": "Sasami (ささみ)",
          "address": "sasami_axis@********.**.**"
        }
      },
      "from": {
        "emailAddress": {
          "name": "Sasami (ささみ)",
          "address": "sasami_axis@********.**.**"
        }
      },
      "toRecipients": [
        {
          "emailAddress": {
            "name": "アライ クマ",
            "address": "arai_kuma@********.**.**"
          }
        }
      ],
      "ccRecipients": [],
      "bccRecipients": [],
      "replyTo": [],
      "flag": {
        "flagStatus": "notFlagged"
      }
    }
  ]
}

 

[データ操作]JSONの解析

JSONの解析を通して動的な値として使えるようにします。

  • コンテンツ
    • @{body('HTTP_要求を送信します-メールをカテゴリで検索')?['value']}
  • スキーマ
    • ※少量データで確認したのみです。null値への対応が必要となるかもしれません
    • {
          "type": "array",
          "items": {
              "type": "object",
              "properties": {
                  "@@odata.etag": {
                      "type": "string"
                  },
                  "id": {
                      "type": "string"
                  },
                  "createdDateTime": {
                      "type": "string"
                  },
                  "lastModifiedDateTime": {
                      "type": "string"
                  },
                  "changeKey": {
                      "type": "string"
                  },
                  "categories": {
                      "type": "array",
                      "items": {
                          "type": "string"
                      }
                  },
                  "receivedDateTime": {
                      "type": "string"
                  },
                  "sentDateTime": {
                      "type": "string"
                  },
                  "hasAttachments": {
                      "type": "boolean"
                  },
                  "internetMessageId": {
                      "type": "string"
                  },
                  "subject": {
                      "type": "string"
                  },
                  "bodyPreview": {
                      "type": "string"
                  },
                  "importance": {
                      "type": "string"
                  },
                  "parentFolderId": {
                      "type": "string"
                  },
                  "conversationId": {
                      "type": "string"
                  },
                  "conversationIndex": {
                      "type": "string"
                  },
                  "isDeliveryReceiptRequested": {
                      "type": [
                          "null",
                          "boolean"
                      ]
                  },
                  "isReadReceiptRequested": {
                      "type": "boolean"
                  },
                  "isRead": {
                      "type": "boolean"
                  },
                  "isDraft": {
                      "type": "boolean"
                  },
                  "webLink": {
                      "type": "string"
                  },
                  "inferenceClassification": {
                      "type": "string"
                  },
                  "body": {
                      "type": "object",
                      "properties": {
                          "contentType": {
                              "type": "string"
                          },
                          "content": {
                              "type": "string"
                          }
                      }
                  },
                  "sender": {
                      "type": "object",
                      "properties": {
                          "emailAddress": {
                              "type": "object",
                              "properties": {
                                  "name": {
                                      "type": "string"
                                  },
                                  "address": {
                                      "type": "string"
                                  }
                              }
                          }
                      }
                  },
                  "from": {
                      "type": "object",
                      "properties": {
                          "emailAddress": {
                              "type": "object",
                              "properties": {
                                  "name": {
                                      "type": "string"
                                  },
                                  "address": {
                                      "type": "string"
                                  }
                              }
                          }
                      }
                  },
                  "toRecipients": {
                      "type": "array",
                      "items": {
                          "type": "object",
                          "properties": {
                              "emailAddress": {
                                  "type": "object",
                                  "properties": {
                                      "name": {
                                          "type": "string"
                                      },
                                      "address": {
                                          "type": "string"
                                      }
                                  }
                              }
                          },
                          "required": [
                              "emailAddress"
                          ]
                      }
                  },
                  "ccRecipients": {
                      "type": "array"
                  },
                  "bccRecipients": {
                      "type": "array"
                  },
                  "replyTo": {
                      "type": "array"
                  },
                  "flag": {
                      "type": "object",
                      "properties": {
                          "flagStatus": {
                              "type": "string"
                          }
                      }
                  }
              },
              "required": [
                  "@@odata.etag",
                  "id",
                  "createdDateTime",
                  "lastModifiedDateTime",
                  "changeKey",
                  "categories",
                  "receivedDateTime",
                  "sentDateTime",
                  "hasAttachments",
                  "internetMessageId",
                  "subject",
                  "bodyPreview",
                  "importance",
                  "parentFolderId",
                  "conversationId",
                  "conversationIndex",
                  "isDeliveryReceiptRequested",
                  "isReadReceiptRequested",
                  "isRead",
                  "isDraft",
                  "webLink",
                  "inferenceClassification",
                  "body",
                  "sender",
                  "from",
                  "toRecipients",
                  "ccRecipients",
                  "bccRecipients",
                  "replyTo",
                  "flag"
              ]
          }
      }

 

特定のフォルダだけ検索・特定のフォルダを除外したい

ここまでのフローではすべてのメールフォルダが検索対象になります。「削除済みアイテム」フォルダも例外ではありません。この為削除したはずのメールも検索結果に表示される可能性がある点は注意が必要です。

※In-Place Archive は対象外でした

 

検索条件にparentFolderId を指定することで対象フォルダまたは対象外フォルダを指定することも可能でした。その為にはメールフォルダのIdが必要になり別のGraph APIを利用する必要があります。

learn.microsoft.com

 

たとえばこのようなフローで「削除済みアイテム」フォルダを除外した検索が可能でした。

参考にしたページ

Graph API公式リファレンスのチェックは必須です。

learn.microsoft.com

 

learn.microsoft.com

 

learn.microsoft.com

 

さいごに

今回もGraph APIが[Office 365 Outlook] コネクタの「HTTP要求を送信します」アクションで使えることを確認しただけですが、備忘録的にまとめてみました。