PowerAutomate: Sharepoint にアップロードしたファイルの名前を、機密等級を付けるか判断をして変更させる。

# 背景

機密文書の取り扱いとして、明示的に機密文書とする必要がある。
その結果・・弊社では、

- 全ファイルに対して機密等級をファイル名に付加する。という規則が・・ :sob: 

いや・・社内文書全部社外秘以上なんだから、基本は社外秘扱いにしてくれませんかね・・と思ったんですが、まぁ規則は規則。

ということで、解決策の一つとして、Automate 用意してみた・・ :sweat: 

# 結果例

こんな感じでファイルをアップロードすると

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/120072/b5f89830-4401-d760-6b55-5416e60f6b0c.png)

作成者にチャットが飛んできて

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/120072/8687ce3f-73c2-7a86-28eb-b2e088be43d5.png)

判断をすると、こんな感じで修正してくれる。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/120072/d80a50a6-3bbe-2d55-835c-f873956e0700.png)

# 実装例

ファイル作成のトリガーを用意したら、

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/120072/042eb736-9b1e-2a08-3fc8-83f054444e96.png)

1. トリガーの input を再利用用に保持
1. Teams チャットへ飛ばす選択肢を生成してメッセージ作成
1. Teams チャットで判断をファイルアップロード者に確認
1. ファイル名変更する場合のみ
    1. REST でファイル名変更

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/120072/44408972-ad08-5308-2144-a880f419cc08.png)

試す場合は、ファイル作成のトリガーを用意したら、以下を使ってアクションを追加すればOK

若干の修正は必要だけど、Trigger input を再利用してるのでそこそこ簡単に利用できるはず

```json: アクション実装例
{"id":"24ee84c4-2181-4531-b855-35a228396dac","brandColor":"#8C3900","connectionReferences":{"shared_teams_1":{"connection":{"id":"/providers/Microsoft.PowerApps/apis/shared_teams/connections/shared-teams-864099d5-54a7-4002-93b6-565ecf481128"}},"shared_teams":{"connection":{"id":"/providers/Microsoft.PowerApps/apis/shared_teams/connections/shared-teams-16d937a3-3608-4ff1-826c-5e345b32810f"}},"shared_sharepointonline":{"connection":{"id":"/providers/Microsoft.PowerApps/apis/shared_sharepointonline/connections/shared-sharepointonl-15e7a58d-6555-418d-a955-a6b286a76a7b"}}},"connectorDisplayName":"Control","icon":"data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzIiIGhlaWdodD0iMzIiIHZlcnNpb249IjEuMSIgdmlld0JveD0iMCAwIDMyIDMyIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPg0KIDxwYXRoIGQ9Im0wIDBoMzJ2MzJoLTMyeiIgZmlsbD0iIzhDMzkwMCIvPg0KIDxwYXRoIGQ9Im04IDEwaDE2djEyaC0xNnptMTUgMTF2LTEwaC0xNHYxMHptLTItOHY2aC0xMHYtNnptLTEgNXYtNGgtOHY0eiIgZmlsbD0iI2ZmZiIvPg0KPC9zdmc+DQo=","isTrigger":false,"operationName":"Scope_RenameFile","operationDefinition":{"type":"Scope","actions":{"Compose_CheckForTriggerConditionResult":{"type":"Compose","inputs":"@not(startsWith(triggerOutputs()?['body']['{FilenameWithExtension}'], '('))","runAfter":{},"metadata":{"operationMetadataId":"fdf60cf9-8a54-440f-918c-8d14749f944a"}},"Compose_dataset":{"type":"Compose","inputs":"@trigger()?['inputs/parameters/dataset']","runAfter":{"Compose_CheckForTriggerConditionResult":["Succeeded"]},"metadata":{"operationMetadataId":"e9ae559a-bbcf-43cf-bbc8-f056f5cbb957"}},"Compose_folderPath":{"type":"Compose","inputs":"@trigger()?['inputs/parameters/folderPath']","runAfter":{"Compose_dataset":["Succeeded"]},"metadata":{"operationMetadataId":"18f10cff-73f9-483d-823f-df0157a04561"}},"Compose_table":{"type":"Compose","inputs":"@trigger()?['inputs/parameters/table']","runAfter":{"Compose_folderPath":["Succeeded"]},"metadata":{"operationMetadataId":"e8dc06ec-9302-4094-9ce3-9c5804cdf927"}},"Compose_folderPath_Absolute":{"type":"Compose","inputs":"@{outputs('Compose_dataset')}@{outputs('Compose_folderPath')}","runAfter":{"Compose_table":["Succeeded"]},"metadata":{"operationMetadataId":"934383e8-55ab-4640-b4d1-69d321894289"}},"Compose_ExampleOfRenaming":{"type":"Compose","inputs":[{"title":"(社外秘)@{triggerOutputs()?['body/{FilenameWithExtension}']}","value":"Conf.JP"},{"title":"(conf.)@{triggerOutputs()?['body/{FilenameWithExtension}']}","value":"Conf.EN"},{"title":"何もしない","value":"None"}],"runAfter":{"Compose_folderPath_Absolute":["Succeeded"]},"metadata":{"operationMetadataId":"7c1e68eb-06df-41d8-83bf-6c2db071b174"}},"Compose_Message":{"type":"Compose","inputs":{"type":"AdaptiveCard","$schema":"http://adaptivecards.io/schemas/adaptive-card.json","version":"1.4","msteams":{"width":"full"},"body":[{"type":"TextBlock","text":"アップロードファイル名確認","wrap":true,"color":"Accent"},{"type":"TextBlock","text":"以下のフォルダに、ファイルが作られました。変更名を選択肢て、確定してください。","wrap":true},{"type":"ActionSet","actions":[{"type":"Action.OpenUrl","title":"@{outputs('Compose_folderPath_Absolute')}","url":"@{outputs('Compose_folderPath_Absolute')}","iconUrl":"https://static2.sharepointonline.com/files/fabric-cdn-prod_20210930.001/assets/brand-icons/product/svg/sharepoint_20x1.svg"},{"type":"Action.OpenUrl","title":"@{triggerOutputs()?['body/{FilenameWithExtension}']}","iconUrl":"https://statics.teams.cdn.office.net/evergreen-assets/personal-expressions/v2/assets/emoticons/3299_circledideographsecret/default/40_f.png","url":"@{triggerOutputs()?['body/{Link}']}"}]},{"type":"Input.ChoiceSet","choices":"@outputs('Compose_ExampleOfRenaming')","placeholder":"機密等級を選択しよう","id":"ConfChoice"},{"type":"ActionSet","actions":[{"type":"Action.Submit","title":"確定する（変更 or 無視）"}]}]},"runAfter":{"Compose_ExampleOfRenaming":["Succeeded"]},"metadata":{"operationMetadataId":"389c2c26-85f8-4a54-b6e1-71d27a3077d2"}},"Post_adaptive_card_and_wait_for_a_response":{"type":"OpenApiConnectionWebhook","inputs":{"host":{"connectionName":"shared_teams_1","operationId":"PostCardAndWaitForResponse","apiId":"/providers/Microsoft.PowerApps/apis/shared_teams"},"parameters":{"poster":"Flow bot","location":"Chat with Flow bot","body/body/messageBody":"@outputs('Compose_Message')","body/body/updateMessage":"ありがとうございました。 :D","body/body/recipient/to":"@{triggerOutputs()?['body/Author/Email']};"},"authentication":"@parameters('$authentication')"},"runAfter":{"Compose_Message":["Succeeded"]},"metadata":{"operationMetadataId":"9d2df2bc-1036-46dd-85bf-f714980f08af"}},"Compose_ChosenResult":{"type":"Compose","inputs":"@body('Post_adaptive_card_and_wait_for_a_response')['data']","runAfter":{"Post_adaptive_card_and_wait_for_a_response":["Succeeded"]},"metadata":{"operationMetadataId":"68513048-75c9-46b3-88e1-45c4b48571b9"}},"Condition":{"type":"If","expression":{"or":[{"equals":["@empty(outputs('Compose_ChosenResult'))",true]},{"equals":["@outputs('Compose_ChosenResult')['ConfChoice']","None"]}]},"actions":{},"runAfter":{"Compose_ChosenResult":["Succeeded"]},"else":{"actions":{"Send_an_HTTP_request_to_SharePoint":{"type":"OpenApiConnection","inputs":{"host":{"connectionName":"shared_sharepointonline","operationId":"HttpRequest","apiId":"/providers/Microsoft.PowerApps/apis/shared_sharepointonline"},"parameters":{"dataset":"@outputs('Compose_dataset')","parameters/method":"POST","parameters/uri":"_api/web/lists/GetById('@{outputs('Compose_table')}')/items(@{triggerOutputs()?['body/ID']})/validateUpdateListItem","parameters/body":"{\n    \"formValues\":[\n\t{\n\t    \"FieldName\": \"FileLeafRef\",\n\t    \"FieldValue\": \"@{body('Filter_array')[0]['title']}\"\n\t}\n    ]\n}"},"authentication":"@parameters('$authentication')"},"runAfter":{"Filter_array":["Succeeded"]},"metadata":{"operationMetadataId":"c34fdb89-0113-47b0-8a8e-84432b28d951"}},"Filter_array":{"type":"Query","inputs":{"from":"@outputs('Compose_ExampleOfRenaming')","where":"@equals(item()['value'], outputs('Compose_ChosenResult')['ConfChoice'])"},"runAfter":{},"metadata":{"operationMetadataId":"1760b36e-d95c-496f-9f6d-1ff7fe988f8d"}}}},"description":"未選択 or 何もしない、選択時は処理しない","metadata":{"operationMetadataId":"6b2d5528-20fb-41a8-a4ab-03e2ed44cab2"}}},"runAfter":{},"metadata":{"operationMetadataId":"cbfc4e6f-2173-4bdf-ab01-c8609d1ef30d"}}}
```

https://qiita.com/baku2san/items/ad4f7fa744d104b23f50

# あとがき

とはいえ・・これ、Trigger が サイト単位なので、全サイトに展開するのがだるい・・ :sweat: 

複数サイトに同時に仕掛けられるトリガー、とかって出来ないものですかね。