Forum Discussion
Using PnP PowerShell to attach a file to an item
Can somepone provide an example of a successful upload of a file as an attachment to a list item using PnP PowerShell? I am struggling getting $item.attachmentFiles.add() to work. I don't seem to be able to build a correct Microsoft.SharePoint.Client.AttachmentCreationInformation object.
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
Hi Scott Oxendine,
This should work (https://sharepoint.stackexchange.com/questions/177832/attaching-a-file-to-a-list-item-using-csom):
var attInfo = new AttachmentCreationInformation(); attInfo.FileName = StringFile.DestinationFileName; attInfo.ContentStream = new MemoryStream(System.IO.File.ReadAllBytes(StringFile.FileAtDestinationPath)); Attachment att = item.AttachmentFiles.Add(attInfo); ctx.Load(att); ctx.ExecuteQuery();
I know this is the C# equivalent but it shouldn't be too difficult to do this in Powershell the same way?
Are you able to share your code?
Also where does your attachment live? It need to exist on a local drive.
- Scott OxendineBrass Contributor
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()
}
- Manidurai MohanamariappanIron Contributor
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