1

powershell と Sharepoint 2013 CSOM を使用して、あるアイテムの添付ファイルを別のリストの新しいアイテムにコピーしようとしています。新しいアイテムの添付ファイル フォルダーを正常に生成できたので、理論的には、古い添付ファイル フォルダーから新しいファイルにファイルを移動するだけで済みます。CopyTo と MoveTo は、リスト内のファイルを移動する場合にのみ機能するように思われるため、サイト コンテキストで OpenBinaryDirect と SaveBinaryDirect を使用することを考えました。ただし、powershell では、これらのメソッドのいずれかを呼び出すと、次のエラーが発生します。

$attachments = $item.AttachmentFiles
if($attachments.Count -gt 0)
{
    #Creates a temporary attachment for the new item to genereate a folder, will be deleted later.
    $attCI = New-Object Microsoft.SharePoint.Client.AttachmentCreationInformation
    $attCI.FileName = "TempAttach"
    $enc = New-Object System.Text.ASCIIEncoding
    $buffer = [byte[]] $enc.GetBytes("Temp attachment contents")
    $memStream = New-Object System.IO.MemoryStream (,$buffer)
    $attCI.contentStream = $memStream
    $newItem.AttachmentFiles.Add($attCI)

    $ctx.load($newItem)
    $sourceIN = $sourceList.Title
    $archIN = $archList.Title
    $sourcePath = "/" + "Lists/$sourceIN/Attachments/" + $item.Id
    $archPath = "/" + "Lists/$archIN/Attachments/" + $newItem.Id
    $sFolder = $web.GetFolderByServerRelativeUrl($sourcePath)
    $aFolder = $web.GetFolderByServerRelativeURL($archPath)
    $ctx.load($sFolder)
    $ctx.load($aFolder)
    $ctx.ExecuteQuery()
    $sFiles = $sFolder.Files
    $aFiles = $aFolder.Files
    $ctx.load($sFiles)
    $ctx.load($aFiles)
    $ctx.ExecuteQuery()
    foreach($file in $sFiles)
    {
        $fileInfo = [Microsoft.SharePoint.Client.File].OpenBinaryDirect($ctx, $file.ServerRelativeUrl)
        [Microsoft.Sharepoint.Client.File].SaveBinaryDirect($ctx, $archPath, $fileInfo.Stream, $true)
    }
}
$ctx.ExecuteQuery()

BinaryDirect メソッドを機能させるためのヘルプ、または powershell + CSOM を使用してリスト間で添付ファイルをコピーするための一般化された戦略に関するヘルプをいただければ幸いです。

4

1 に答える 1