0

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: enter image description here

However, in the Gallery view for the same list, the image is blank (instead of undefined): enter image description here

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?

1 Answer 1

1

As per Add/Update image columns in SharePoint/Microsoft Lists using PnP PowerShell article, you can update the image column using:

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'}"}

You have to set the serverRelativeUrl as well as the other properties of image column.


In order to show the image properly in gallery view, make sure image is referenced like below in gallery view JSON:

[$fieldName.serverRelativeUrl]

Where fieldName is internal name of the image column. You can get the internal name of your Image column by following this article: How to find the Internal name of columns in SharePoint Online?

Source: SharePoint Online: All you need to know about New Image column type - check comments section

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.