I have a SharePoint Online list column that accepts an image called Image. I would like to populate this content using PowerShell, specifically using the PnP module, as the rest of the script I'm writing is leveraging this already.
Finding this question asked at Add/Change image on a SharePoint List image column, I'm getting partial success when uploading the content.
PowerShell code that creates the list item, uploads the file, and sets the list item's Image field to the uploaded content:
$AddRecord = Add-PnPListItem -List $list -Values @{
Firstname = $Comparison.FirstName;
LastName = $Comparison.LastName;
Initials = $Comparison.Initials;
Office = $Comparison.Office;
Department = $Comparison.Department;
OfficePhone = $Comparison.OfficePhone;
MobilePhone=$Comparison.MobilePhone;
Title="$($Comparison.Firstname) $($Comparison.LastName)"
}
$InsertID = $AddRecord.Id
$EmployeePhoto = Join-Path "C:\Photos" "$($Comparison.Initials).JPG"
if (Test-Path $EmployeePhoto) {
$FileUploaded = Add-PnPFile -Path $EmployeePhoto -Folder "/sites/intranet/SiteAssets/Employee Directory Photos"
// $FileUploaded
Set-PnPListItem -List $list -Identity $InsertID -Values @{Image = "{serverRelativeUrl: '$($FileUploaded.ServerRelativeUrl)'}"}
}
When reviewing the individual list item through the web UI, I can see the image:
However, in the Gallery view for the same list, the image is blank (instead of undefined):
If I manually upload the image to the record, the Gallery view works just fine, and in this case, I find the image in /SiteAssets/Lists/<some guid>/filename.jpg
.
How do I get an image to be uploaded correctly for use in a Gallery view?