Forum Discussion
Using PnP PowerShell to attach a file to an item
- Jun 17, 2017
Try this script
function writeItem( $itemTitle, $itemOther, $attachments ) { # check if file exists first $items=Add-PnPListItem -List testlist $newListItem = Set-PnPListItem -Identity $Items.Id -List testlist -Values @{"Title" = $itemTitle; "other" = $itemOther} for ($a=0; $a -lt $attachments.length; $a++) { #Write-host " " $attachments[$a] writeAttachment -item $items -fileWithPath $attachments[$a] } } function writeAttachment($item, $fileWithPath) { $ctx=Get-PnPContext $memoryStream = New-Object IO.FileStream($fileWithPath,[System.IO.FileMode]::Open) $fileName = Split-Path $fileWithPath -Leaf $attachInfo = New-Object -TypeName Microsoft.SharePoint.Client.AttachmentCreationInformation $attachInfo.FileName = $fileName $attachInfo.ContentStream = $memoryStream $attFile = $item.attachmentFiles.add($attachInfo) $ctx.load($attFile) $ctx.ExecuteQuery() } Connect-PnPOnline -url https://tenantname.sharepoint.com/sites/contosobeta $Att=@("C:\copyfile.csv","C:\AllSubsitegrouppermission.csv","C:\AllUserWithLicenseType.csv") writeItem -itemTitle "test6789" -itemOther "efgh" -attachments $Att
This function accepts a couple of string fields and an array of strings that list the full path to the files. The files are located locally. They are just small, but not empty, txt files. (ex: c:\test\row1-att1.txt).
function writeItem(
$itemTitle,
$itemOther,
$attachments
) {
# check if file exists first
$newListItem = Add-PnPListItem -List $spoList -Values @{"Title" = $itemTitle; "other" = $itemOther}
for ($a=0; $a -lt $attachments.length; $a++) {
#Write-host " " $attachments[$a]
writeAttachment -item $newListItem -fileWithPath $attachments[$a]
}
All works until it calls this function that actually writes the attachment:
function writeAttachment($item, $fileWithPath)
{
$memoryStream = New-Object -TypeName System.IO.MemoryStream ([System.IO.File]::ReadAllBytes($fileWithPath))
$fileName = Split-Path $fileWithPath -Leaf
$attachInfo = New-Object -TypeName Microsoft.SharePoint.Client.AttachmentCreationInformation
$attachInfo.FileName = $fileName
$attachInfo.ContentStream = $MemoryStream
$attFile = $item.attachmentFiles.add($attachInfo)
$siteCtx.load($attFile)
$siteCtx.ExecuteQuery()
}
Try this script
function writeItem( $itemTitle, $itemOther, $attachments ) { # check if file exists first $items=Add-PnPListItem -List testlist $newListItem = Set-PnPListItem -Identity $Items.Id -List testlist -Values @{"Title" = $itemTitle; "other" = $itemOther} for ($a=0; $a -lt $attachments.length; $a++) { #Write-host " " $attachments[$a] writeAttachment -item $items -fileWithPath $attachments[$a] } } function writeAttachment($item, $fileWithPath) { $ctx=Get-PnPContext $memoryStream = New-Object IO.FileStream($fileWithPath,[System.IO.FileMode]::Open) $fileName = Split-Path $fileWithPath -Leaf $attachInfo = New-Object -TypeName Microsoft.SharePoint.Client.AttachmentCreationInformation $attachInfo.FileName = $fileName $attachInfo.ContentStream = $memoryStream $attFile = $item.attachmentFiles.add($attachInfo) $ctx.load($attFile) $ctx.ExecuteQuery() } Connect-PnPOnline -url https://tenantname.sharepoint.com/sites/contosobeta $Att=@("C:\copyfile.csv","C:\AllSubsitegrouppermission.csv","C:\AllUserWithLicenseType.csv") writeItem -itemTitle "test6789" -itemOther "efgh" -attachments $Att
- Scott OxendineJun 20, 2017Brass Contributor
Thank you, Manidurai! That worked!