Setting a SharePoint Image Column Value with PnP PowerShell

Edit: There’s an even easier way to do this via a new commandlet added to PnP PowerShell 1.12 on November 10, 2022.

The PnP Set-PnPImageListItemColumn commandlet allows you to upload a new image or reference an existing one, and I’d highly recommend that over the below method. Having said that, I can also confirm the commandlet correctly sets the id attribute in the JSON, which was the root of the initial issue.


Back in 2022 I worked on a PowerApp with @thechriskent where we were using an Image column on a SharePoint list, and were automating setting the value of it for hundreds of items that already existed in the list, since we had a batch of images we’d uploaded to the list’s related “SiteAssests/{ListID}” folder, and wanted to quickly wire those images up to their related list items (the filenames of our images were based on metadata on the list items so it was easy to programmatically determine which image we wanted to reference).

I started out by referencing Ganesh Sanap’s “Add/Update Image in SharePoint Image column” article and at first, it seemed like were were good to go, as the images showed up correctly in SharePoint Online. However, upon trying to view the images in the related PowerApp (desktop browser and native mobile app), we noticed that the images failed to appear. Furthermore, checking the Microsoft Lists iOS app, the images were once again missing.

Digging into the value of an image column set manually by editing the value in SharePoint Online, I noticed the JSON value for the image column also contained an ‘id’ attribute that wasn’t in Ganesh’s article. Utilizing a sample, I was able to confirm that on values set in the UI, that ‘id’ attribute was actually the ‘UniqueId’ of the related file.

Based on that, a slight tweak to the script in his article will help wire up the value:

# Connect to SharePoint Online site  
Connect-PnPOnline -Url $siteUrl -Interactive

# Get uniqueid of file you're referencing (without this part your image won't appear in PowerApps (browser or mobile app) or Microsoft Lists (iOS app))
$imageFileUniqueId = (Get-PnPFile -Url "SiteAssets/Lists/dbc6f551-252b-462f-8002-c8f88d0d12d5/PnP-PowerShell-Blue.png" -AsListItem)["UniqueId"]

# Create new list item with image column
Add-PnPListItem -List "Logo Universe" -Values @{"Title" = "PnP PowerShell"; "Image" = "{'type':'thumbnail','fileName':'PnP-PowerShell-Blue.png','fieldName':'Image','serverUrl':'https://contoso.sharepoint.com','serverRelativeUrl':'/sites/SPConnect/SiteAssets/Lists/dbc6f551-252b-462f-8002-c8f88d0d12d5/PnP-PowerShell-Blue.png', 'id':'$($imageFileUniqueId)'}"}

# Update list item with image column
Set-PnPListItem -List "Logo Universe" -Identity 12 -Values @{"Image" = "{'type':'thumbnail','fileName':'PnP-PowerShell-Blue.png','fieldName':'Image','serverUrl':'https://contoso.sharepoint.com','serverRelativeUrl':'/sites/SPConnect/SiteAssets/Lists/dbc6f551-252b-462f-8002-c8f88d0d12d5/PnP-PowerShell-Green.png', 'id':'$($imageFileUniqueId)'}"}

If you were previously experiencing a similar issue, I hope this article was able to help you resolve it.

Cheers,
Matt

Matt Jimison

Microsoft 365 Geek - Husband, father, lover of basketball, football, smoking / grilling, music, movies, video games, and craft beer!

Leave a Reply

Your email address will not be published. Required fields are marked *