5
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?

Power Automate でSharePointリストのフォルダーを操作する

Posted at

はじめに

SharePointリストでも設定を変えればフォルダーを作成することができます。

設定

リストの設定を開き、詳細設定から[新しいフォルダー]コマンドを表示するはいに変更します。

image.png
image.png

設定を変更すると新規フォルダーを作成することができるようになります。

image.png

Power Automate で操作する

フォルダー操作のアクションはあまり豊富に用意はされていませんので、標準アクションでそのまま実行できるものと、SharePoint に HTTP 要求を送信しますアクションを使用して実行するものがあります。

フォルダーの作成

新しくフォルダーを作成するのは、標準アクションで実行できます。

image.png

サイト・リストをプルダウンから指定し、/区切りでパスを指定して実行できます。

ドキュメントライブラリでの操作と同様です。

フォルダー内のアイテムを一覧取得する

こちらも標準アクションで実行できます。

image.png

詳細パラメーターからエントリをフォルダーに制限するでフォルダーのフルパス(完全パス)を指定しましょう。

image.png

フォルダー内にアイテムを作成する

項目の作成アクションでは作成先のフォルダーを指定することができません。

image.png

フォルダー内にアイテムを作成したい場合は、HTTPリクエストを使用します。

image.png

URI(リスト名で指定)
_api/web/lists/GetByTitle('とあるリスト')/AddValidateUpdateItemUsingPath

もしくは

URI(リストIdで指定)
_api/web/lists('リストのId')/AddValidateUpdateItemUsingPath

ボディには以下の形式で保存先のフォルダーと作成する各プロパティを設定します。
image.png

Body
{
    "listItemCreateInfo": {
        "FolderPath": {
            "DecodedUrl": "/sites/toaruSite/@{outputs('新しいフォルダーの作成')?['body/{FullPath}']}"
        }
    },
    "formValues": [
        {
            "FieldName": "Title",
            "FieldValue": "タイトル"
        },
        
        ...

        {
            "FieldName": "ContentType",
            "FieldValue": "アイテム"
        }
    ],
    "bNewDocumentUpdate": false
}

各列の値は配列formValuesで記述するのですが、実際に実行したときのデータサンプルを記載しておきます。(長いので折りたたみで)

様々な列のデータ型で実行したサンプル
Body
{
    "listItemCreateInfo": {
        "FolderPath": {
            "DecodedUrl": "/sites/toaruSite/@{outputs('新しいフォルダーの作成')?['body/{FullPath}']}"
        }
    },
    "formValues": [
        {
            "FieldName": "Title",
            "FieldValue": "タイトル"
        },
        {
            "FieldName": "Text",
            "FieldValue": "テキスト"
        },
        {
            "FieldName": "Choice",
            "FieldValue": "選択肢 1"
        },
        {
            "FieldName": "Date",
            "FieldValue": "2025/7/13"
        },
        {
            "FieldName": "Note",
            "FieldValue": "複数行\nテキスト"
        },
        {
            "FieldName": "User",
            "FieldValue": "[{\"Key\":\"i:0#.f|membership|xxx@outlook.com\",\"DisplayText\":\"A さん\",\"IsResolved\":true,\"Description\":\"xxx@outlook.com\",\"EntityType\":\"User\",\"EntityData\":{\"IsAltSecIdPresent\":false,\"UserKey\":\"i:0h.f|membership|xxx@live.com\",\"Title\":null,\"Email\":\"xxx@outlook.com\",\"MobilePhone\":null,\"ObjectId\":\"d702e418-45ec-4f74-815f-47d6e32f307c\",\"Department\":null},\"MultipleMatches\":[],\"ProviderName\":\"Tenant\",\"ProviderDisplayName\":\"テナント\"}]"
        },
        {
            "FieldName": "Number",
            "FieldValue": "100"
        },
        {
            "FieldName": "Boolean",
            "FieldValue": "1"
        },
        {
            "FieldName": "HyperLink",
            "FieldValue": "https://make.powerautomate.com, Power Automate"
        },
        {
            "FieldName": "Currency",
            "FieldValue": "3.14159265"
        },
        {
            "FieldName": "Place",
            "FieldValue": "{\"Score\":-20,\"EntityType\":\"Unknown\",\"LocationSource\":\"Bing\",\"FormattedAddress\":\"日本\",\"LocationUri\":\"https://www.bingapis.com/api/v6/places/search?q=%e6%9d%b1%e4%ba%ac%e9%83%bd&setLang=ja-JP&dtype=PlaceOrLandmark\",\"Availability\":\"Unknown\",\"UniqueId\":\"https://www.bingapis.com/api/v6/places/search?q=%e6%9d%b1%e4%ba%ac%e9%83%bd&setLang=ja-JP&dtype=PlaceOrLandmark\",\"IsPreviouslyUsed\":false,\"Id\":\"https://www.bingapis.com/api/v6/places/search?q=%e6%9d%b1%e4%ba%ac%e9%83%bd&setLang=ja-JP&dtype=PlaceOrLandmark\",\"DisplayName\":\"東京都\",\"Address\":{\"CountryOrRegion\":\"日本\",\"Type\":\"Unknown\",\"IsInferred\":false},\"Coordinates\":{\"Latitude\":35.689927,\"Longitude\":139.691763}}"
        },
        {
            "FieldName": "Lookup",
            "FieldValue": "1;#item1"
        },
        {
            "FieldName": "ContentType",
            "FieldValue": "アイテム"
        }
    ],
    "bNewDocumentUpdate": false
}

作成したアイテムのIdを調べる

上記を実行すると、formValuesの配列で指定したプロパティの設定結果が、resultに含まれて返されます。

実行結果(body)
"body": {
    "d": {
        "AddValidateUpdateItemUsingPath": {
            "__metadata": {
                "type": "Collection(SP.ListItemFormUpdateValue)"
            },
            "results": [
                {
                    "ErrorCode": 0,
                    "ErrorMessage": null,
                    "FieldName": "Title",
                    "FieldValue": "タイトル",
                    "HasException": false,
                    "ItemId": 0
                },

                ...

                {
                    "ErrorCode": 0,
                    "ErrorMessage": null,
                    "FieldName": "ContentType",
                    "FieldValue": "アイテム",
                    "HasException": false,
                    "ItemId": 0
                },
                {
                    "ErrorCode": 0,
                    "ErrorMessage": null,
                    "FieldName": "Id",
                    "FieldValue": "6",
                    "HasException": false,
                    "ItemId": 0
                }
            ]
        }
    }
}

このうち、最後の部分にIdが含まれているため、配列からIdを抜き出す操作が必要になります。
アレイのフィルター処理FieldNameIdになっているものを抽出し、FieldValueのIdを取り出す作業です。

image.png

アレイのフィルター処理(From)
@{body('SharePoint_に_HTTP_要求を送信します:_アイテムの作成')?['d/AddValidateUpdateItemUsingPath/results']}
アレイのフィルター処理(Filter Query)
@equals(@{item()?['FieldName']},'Id')
作成: アイテムのId
body('アレイのフィルター処理')?[0]?['FieldValue']

フォルダー間でアイテムを移動する

アイテムをフォルダーからフォルダー(もしくはフォルダー外)に移動するのもHTTPリクエストを使用します。

こちらのファイルの移動アクションでは実行できませんでした。
image.png

リクエストは以下のように実行します。
image.png

URI
_api/SP.MoveCopyUtil.MoveFileByPath()
Body
{
    "destPath": {
        "DecodedUrl": "https://v45dm.sharepoint.com/sites/toaruSite/Lists/toaruList/folder1/10_.000"
    },
    "options": {
        "RetainEditorAndModifiedOnMove": true,
        "ShouldBypassSharedLocks": true
    },
    "srcPath": {
        "DecodedUrl": "https://v45dm.sharepoint.com/sites/toaruSite/Lists/toaruList/10_.000"
    }
}

destPathは移動先のフォルダーに移動した後のアイテムの完全パス、srcPathは移動前の完全パスです。
フォルダーのパスとアイテムのIdがあれば作成できそうですが、末尾に謎の_.000がついていたので、項目の取得アクションなどで確認しておく方が無難だと思います。
image.png

image.png

移動後のパスは、フォルダーのパスだけではなく、末尾にそのアイテムのIdを含むパスが必要なようでしたので、アイテムのIdから切り取って指定しています。
image.png

10_.000 のアイテム完全パス末尾の部分を切り出す
last(split(outputs('項目の取得')?['body/{FullPath}'],'/'))

おわりに

リストの各項目にアクセス権を個別で割り当てるのは、管理上非推奨な方法だと聞いて、「じゃあフォルダにまとめたら管理しやすくなるんじゃね?」 と思い調べてみました。

フォルダの操作自体がややこしく、これもまた推奨しかねる方法だとは思うのですが、せっかく調べたので備忘録として残しておきます。

5
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
5
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?