1

I have 400 product items. Previously, we have a common thumbnail image for all products. Now, I added a new image field (thumbnail image), all products I need to assign a common specific thumbnail image.

I wrote PowerShell script but it's not working

$thumbnailImage = Get-Item -Path 'master:\sitecore\media library\xyz\pqr\mno\infotumbnail410PD1'

$itemPath= Get-Item -Path 'master:\sitecore\content\sites\abc'

$itemPath.Editing.BeginEdit()

[Sitecore.Data.Fields.ImageField]$fieldValueLink = $itemPath["Product Image"]
$fieldValueLink= $thumbnailImage 

$itemPath.Editing.EndEdit();

Please suggest me on the same.

Thanks,

3 Answers 3

2

In theory you should just be able to do this:

$item = Get-Item -Path "master:\sitecore\content\sites\abc"
$item.Image = Get-Item -Path "master:\sitecore\media library\xyz\pqr\mno\infotumbnail410PD1"

from this page: https://doc.sitecorepowershell.com/working-with-items

But I couldn't figure out how to do that with fields with a space in them. So I did it like so instead but I think this is a bit hacky as I've created the field value with a string.format which is not very nice.

$thumbnailImage = Get-Item -Path 'master:\sitecore\media library\xyz\pqr\mno\infotumbnail410PD1'

$item= Get-Item -Path 'master:\sitecore\content\sites\abc'

$item.Editing.BeginEdit()

$item.Fields["Listing Image"].Value = [string]::Format("<image mediaid='{0}' />",$thumbnailImage.ID)

$item.Editing.EndEdit();

There must be a better way of doing this though.

1
  • Thank you @Adam Commented May 24, 2022 at 5:08
0

When testing your script, I was having an error with the cast of ImageField and also saw that in RAW values, it was adding the value Sitecore.Data.Items.Item. I did a modification of your script and tested it. Please use the below script. You may need to update it to fit your requirements

$thumbnailImage = Get-Item -Path 'master:\sitecore\media library\xyz\pqr\mno\infotumbnail410PD1'

$itemPath= Get-Item -Path 'master:\sitecore\content\sites\abc'


$itemPath.Editing.BeginEdit()

$itemPath."Product Image" = $thumbnailImage

$itemPath.Editing.EndEdit();
0

$mid = fetching media item id from media library

$m.Editing.BeginEdit()
  $t='<file mediaid="'+ $mid +'" src=' + '"-/media/'+$mGuid+'.ashx" />';
  $m["ProductListing"]= $t
 $m.Editing.EndEdit();

Your Answer

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

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