Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

0
1

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.

【PowerShell】フォルダを指定してリストアイテムに添付ファイルを追加する

Last updated at Posted at 2021-09-02

添付ファイルとして追加するファイルが格納されたフォルダのパスを指定して、Sharepoint カスタムリストのリストアイテムに添付ファイルを追加する方法です。


関連ページ


リストアイテムに添付ファイルを追加する

同名のファイルを添付しようとするとエラーになる為、それを考慮しています。


# 追加するファイルが格納されたフォルダ
$FolderPath = 'C:\Data'
[System.IO.FileInfo[]] $AddFiles = dir $FolderPath -Recurse -File

# リストアイテム情報取得
$ListName = 'Announcements'
$Id = 2
$item = Get-PnPListItem -List $ListName -Id $Id
$AttachmentFiles = Get-PnPProperty -ClientObject $item -Property AttachmentFiles

# 上書きするかどうか
$Overwrite = $true
if ($Overwrite) {
    # 同名のファイルを削除
    $AttachmentFiles | ? FileName -in $AddFiles.Name | % {
        Remove-PnPFile -ServerRelativeUrl $_.ServerRelativeUrl -Force
    } 

    # すべてのファイルを追加
    $AddFiles | % {
        $at = New-Object Microsoft.SharePoint.Client.AttachmentCreationInformation
        $at.FileName =  $_.Name
        $at.ContentStream = $_.OpenRead()
        $item.AttachmentFiles.Add($at)
    }
} else {
    # 添付ファイルに存在しないファイルのみを追加
    $AddFiles | ? Name -notin $AttachmentFiles.FileName | % {
        $at = New-Object Microsoft.SharePoint.Client.AttachmentCreationInformation
        $at.FileName =  $_.Name
        $at.ContentStream = $_.OpenRead()
        $item.AttachmentFiles.Add($at)
    }
}
Invoke-PnPQuery

参考サイト

0
1
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

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?