kwizcom banner advertisement

SharePoint Online: Add Attachment to List Item using PowerShell

Requirement: Add Attachment to SharePoint Online 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

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.