2

I want to add an image to an item using powershell script and here is my code. I don't know what i'm missing..

$item.Editing.BeginEdit()

[Sitecore.Data.Fields.ImageField]$fieldValueLink = $Item.Fields[$templateField.DisplayName]
$fieldValueLink.Width = 1324
$fieldValueLink.Height = 577
$fieldValueLink.Alt="Image"
$fieldValueLink.Value = $itemRow.$fieldTrimmedValue
$item[$templateField.DisplayName] = $itemRow.$fieldTrimmedValue;

$item.Editing.EndEdit() | Out-Null

1 Answer 1

4

Example: The following shows how to retrieve an image from the media library and assign it to the image field on a content item.

$homeItem = Get-Item -Path "master:/content/home"
$homeItem.Image = Get-Item -Path "master:\media library\logo"
$homeItem.PSFields.Image.Alt = "I like turtles"

Example: The following shows both editing mode and automatic properties:

$item.Editing.BeginEdit()   

# Whatever other fields you want...
$item[$templateField.Name]= Get-Item -Path "master:\media library\auto-email"

$item.Editing.EndEdit() | Out-Null    

# This notation does not require the Begin/End edit calls.
$item.PSFields.Image.Alt = "I like turtles"

Resources

3
  • $item[$templateField.Name] = Get-Item -Path "master:\media library\auto-email"; $item[$templateField.PSFields.Image.Alt] = "GoogleLogo"; $item.Editing.EndEdit() | Out-Null on adding this code getting the following error Index operation failed; the array index evaluated to null. $item[$templateField.PSFields.Image.Alt] = "GoogleLogo"; Commented Jul 12, 2019 at 14:54
  • My example automatically puts the item in editing mode whenever it modifies the field. I'll make an update to follow your example. Commented Jul 12, 2019 at 15:11
  • I am getting this error. The property 'Image' cannot be found on this object. Verify that the property exists and can be set.
    – Sitecorer
    Commented Jan 25, 2021 at 15:59

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.