1

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
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

  • $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"; – kohila vani Jul 12 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. – Michael West Jul 12 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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