kwizcom banner advertisement

Add Attachment to List Item in SharePoint using PowerShell

How to Add Attachment to a SharePoint List Item?
To add attachment to SharePoint list item,
  • Navigate to the list, select a list item 
  • Click on "Attach File" button from the "Items" tab of the ribbon. 
  • Browse and select the file to attach. Click OK to attach the file to item. You can also Edit the list item and then attach file.
    powershell add attachment to sharepoint list item

PowerShell to add attachment to sharepoint list item
Here is the PowerShell script to add attachment to list item programmatically in SharePoint
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Custom Function to Add attachment to SharePoint List Item
Function Add-Attachment($Item, $AttachmentPath)
{
  $FileContent = [System.IO.File]::ReadAllBytes($AttachmentPath)
  $Item.Attachments.Add([System.IO.Path]::GetFileName($AttachmentPath), $FileContent)
  $Item.Update()
 
  Write-host "Attachment Added to List Item Successfully!"
}

#Variables
$SiteURL="https://portal.crescent.com/sites/Deals"
$ListName="Tasks"
$ItemID=1
$AttachmentPath="c:\Scripts\ASI-LOG.docx"

$web = Get-SPWeb $SiteURL
$List = $web.Lists[$ListName]
$Item = $list.GetItemById($ItemID)

#Call the function to Add Attachment
Add-Attachment $Item $AttachmentPath
Add Attachment to List Item in SharePoint using PowerShell Add Attachment to List Item in SharePoint using PowerShell Reviewed by Salaudeen Rajack on 6:18 PM Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.