SharePoint Online: Add Attachment to List Item using PowerShell

Requirement: Add Attachment to SharePoint Online List Item

SharePoint Online: How to Add Attachment to List Item?
To add attachment to list item in SharePoint Online,
  • Navigate to the SharePoint Online list >> Select the List Item you want to add attachments
  • Edit List Item >> Scroll down and Click on "Add 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 attachment to list item.

PowerShell to Add an Attachment to List Item:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Add-AttachmentToListItem()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $ItemID,
        [Parameter(Mandatory=$false)] [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 Item
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        $Ctx.Load($List)
        $ListItem = $List.GetItemByID($ItemID)
        $Ctx.Load($ListItem)
        $Ctx.ExecuteQuery()

        #Get All existing attachments
        $AttachmentFiles = $ListItem.AttachmentFiles
        $Ctx.Load($AttachmentFiles)
        $Ctx.ExecuteQuery()

        #Check if attachment file name exists already
        $FileName = Split-Path $AttachmentPath -Leaf
        $AttachmentFile = $AttachmentFiles | where { ($_.FileName -eq $FileName) }
        If($AttachmentFile -eq $Null)
        {
            #Get the Attachment file from local disk
            [Byte[]]$Bytes = [System.IO.File]::ReadAllBytes($AttachmentPath)
            $ContentStream = New-Object -TypeName System.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 Green "Attachment Added to List Item!" 
        }
        else
        {
            write-host -f Yellow "Attachment File Name Exists already!"
        }
    }
    Catch {
        write-host -f Red "Error Adding Attachment to List!" $_.Exception.Message
    }
}

#Set Parameters
$SiteURL= "https://crescent.sharepoint.com"
$ListName="Projects"
$ItemID="5"
$AttachmentPath="C:\Projects\Proposal.docx"

#Call the function to copy list items
Add-AttachmentToListItem -SiteURL $SiteURL -ListName $ListName -ItemID $ItemID -AttachmentPath $AttachmentPath
SharePoint Online: Add Attachment to List Item using PowerShell SharePoint Online: Add Attachment to List Item using PowerShell Reviewed by Salaudeen Rajack on February 01, 2017 Rating: 5

1 comment:

  1. I am getting error on 49thline.

    Exception calling "ExecuteQuery" with "0" argument(s): "parameters
    Parameter name: Specified value is not supported for the parameters parameter."
    At line:36 char:13
    + $Ctx.ExecuteQuery()
    + ~~~~~~~~~~~~~~~~~~~

    If I remove the line, script has been ran successfully.
    But File has not been uploaded.Please help.

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.