Requirement: Add Attachment to SharePoint Online List Item

Attachments are a nifty manner to go on related documents and files together with your listing items in SharePoint Online. In this post, we'll walk y'all through the process of adding an attachment to a SharePoint Online list particular.

To add an attachment to a listing detail in SharePoint Online, follow these steps:

  • Navigate to the SharePoint Online list >> Select the List Item you want to add attachments
  • Edit Listing Particular >> Ringlet down and Click on "Add together Attachments" link in the Edit pane.
    sharepoint online add attachment to list item powershell
  • This opens browse dialog window., Select attachments to add
  • Click on Save to complete adding zipper to list particular.

SharePoint Online list detail zipper size limit: The file size limit on SharePoint Online list attachments is 250 MB!

PowerShell to Add an Attachment to Listing Detail:

If you want to automate the process of attaching files to a list, PowerShell can make the process piece of cake. Let'due south walk y'all through how to attach a file to a specific list particular in your SharePoint Online list using PowerShell. This is a handy style to adhere multiple files to list items without having to open the item editor.

Here is the PowerShell script to add attachments to the list detail:

#Load SharePoint CSOM Assemblies Add-Blazon -Path "C:\Plan Files\Mutual Files\Microsoft Shared\Spider web Server Extensions\16\ISAPI\Microsoft.SharePoint.Customer.dll" Add-Type -Path "C:\Programme Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Customer.Runtime.dll"  Function Add-AttachmentToListItem() {     param     (         [Parameter(Mandatory=$true)] [string] $SiteURL,         [Parameter(Mandatory=$true)] [string] $ListName,         [Parameter(Mandatory=$truthful)] [string] $ItemID,         [Parameter(Mandatory=$imitation)] [string] $AttachmentPath     )         Try {         #Setup Credentials to connect         $Cred = Get-Credential         $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)              #Setup the context         $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)         $Ctx.Credentials = $Cred          #Get the List & List Detail         $List = $Ctx.Web.Lists.GetByTitle($ListName)         $Ctx.Load($List)         $ListItem = $List.GetItemByID($ItemID)         $Ctx.Load($ListItem)         $Ctx.ExecuteQuery()          #Go All existing attachments         $AttachmentFiles = $ListItem.AttachmentFiles         $Ctx.Load($AttachmentFiles)         $Ctx.ExecuteQuery()          #Check if zipper file proper noun exists already         $FileName = Split-Path $AttachmentPath -Leafage         $AttachmentFile = $AttachmentFiles | where { ($_.FileName -eq $FileName) }         If($AttachmentFile -eq $Null)         {             #Get the Attachment file from local disk             [Byte[]]$Bytes = [Organisation.IO.File]::ReadAllBytes($AttachmentPath)             $ContentStream = New-Object -TypeName Organization.IO.MemoryStream -ArgumentList @(,$Bytes)                  #Create Attachment object             $AttachmentCreation = New-Object Microsoft.SharePoint.Client.AttachmentCreationInformation             $AttachmentCreation.ContentStream = $ContentStream             $AttachmentCreation.FileName = $FileName             [Void]$ListItem.AttachmentFiles.Add($AttachmentCreation)             $Ctx.ExecuteQuery()              write-host  -f Light-green "Attachment Added to Listing Particular!"          }         else         {             write-host -f Yellow "Attachment File Proper name Exists already!"         }     }     Catch {         write-host -f Cherry "Error Adding Attachment to List!" $_.Exception.Bulletin     } }  #Set Parameters $SiteURL= "https://crescent.sharepoint.com" $ListName="Projects" $ItemID="5" $AttachmentPath="C:\Projects\Proposal.docx"  #Telephone call the office to copy list items Add-AttachmentToListItem -SiteURL $SiteURL -ListName $ListName -ItemID $ItemID -AttachmentPath $AttachmentPath              

PnP PowerShell to Add Attachment to List Detail

Let'due south add together an attachment to the SharePoint Online list item with PnP PowerShell.

#Set Variables $SiteURL = "https://crescent.sharepoint.com/sites/Marketing" $ListName = "Projects" $ItemID ="2" $FilePath ="C:\Docs\UserInfo.csv"  #Connect to PnP Online Connect-PnPOnline -Url $SiteURL -Credentials (Become-Credential)  #Get the List Detail $Item  = Get-PnPListItem -List $ListName -Id $ItemID  #Become the File and Add to List Item Attachment $FileStream = New-Object IO.FileStream($FilePath,[System.IO.FileMode]::Open)  $AttachmentInfo = New-Object -TypeName Microsoft.SharePoint.Customer.AttachmentCreationInformation  $AttachmentInfo.FileName =  Split-Path $FilePath -Leafage  $AttachmentInfo.ContentStream = $FileStream $AttchedFile = $Item.AttachmentFiles.Add($AttachmentInfo)  Invoke-PnPQuery $FileStream.Close()              

How near adding multiple attachments to the listing item? Permit's add all files from a given folder to SharePoint Online.

#Fix Variables $SiteURL = "https://crescent.sharepoint.com/sites/pmo" $ListName = "Projects" $ItemID ="2" $FolderPath = "C:\Temp\Docs"  #Connect to PnP Online Connect-PnPOnline -Url $SiteURL -Interactive  #Get the List Item $ListItem  = Get-PnPListItem -Listing $ListName -Id $ItemID  #Function to Add together all files as a zipper to listing item Role Add-AttachmentsFromFolder($ListItem, $FolderPath) {     #Get All existing attachments from list detail     $AttachmentFiles = Get-PnPProperty -ClientObject $ListItem -Property "AttachmentFiles"      #Get the File and Add to List Item Attachment     ForEach ($File in  (Get-ChildItem $FolderPath -File))     {         $AttachmentFile = $AttachmentFiles | Where { ($_.FileName -eq $File.Name) }         If($AttachmentFile -eq $Null)         {             $AttachmentInfo = New-Object -TypeName Microsoft.SharePoint.Client.AttachmentCreationInformation              $AttachmentInfo.FileName =  $File.Name             $AttachmentInfo.ContentStream = $File.OpenRead()             $AttchedFile = $ListItem.AttachmentFiles.Add($AttachmentInfo)              Invoke-PnPQuery             Write-host -f Green "Added Attachment File:"$File.FullName         }         Else         {             write-host -f Yellowish "Attachment '$($File.Name)' Exists already!"         }     } } #Telephone call the function to add all attachments from binder Add-AttachmentsFromFolder $ListItem $FolderPath              

My other post on downloading attachments from SharePoint Online listing: How to Download Attachments from SharePoint Online List using PowerShell?

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Feel. He loves sharing his knowledge and experiences with the SharePoint community, through his real-earth articles!