I'm using the following script to update a specific field in all the children of the item right-clicked in my content tree. It works but the update is not applied to the item clicked with my context menu. It is only applied to children.

Does anyone know how to fix this ?

Get-ChildItem -Recurse . | Where-Object { $_.Fields["__Enable item fallback"] -ne $null } | ForEach-Object {

    $_.Editing.BeginEdit()
    $_.Fields["__Enable item fallback"].Value = "1";
    $_.Editing.EndEdit()

}
  • 1
    You are calling Get-ChildItem cmdlet that's why update is only applied to children. You should do the same also with Get-Item cmdlet – Peter Procházka Mar 19 at 16:18
up vote 2 down vote accepted

You are calling Get-ChildItem cmdlet that's why update is only applied to children. You should do the same also with Get-Item cmdlet to make it work also for current item.

Should be like this:

$item = Get-Item .
$item.Editing.BeginEdit()
$item.Fields["__Enable item fallback"].Value = "1"
$item.Editing.EndEdit()

. is representing current item.

How to add custom task in context menu with SPE:

https://www.sitecorenutsbolts.net/2016/11/01/Right-Click-Unlock-Item-with-Sitecore-PowerShell-Extensions/

See more about supported cmdlets here:

https://blog.najmanowicz.com/2014/10/12/working-with-sitecore-items-in-powershell-extensions/

  • Thank you ! How can i get the path to item ? The item in question is clicked with the context menu – Slrg Mar 19 at 16:39
  • See updated answer – Peter Procházka Mar 19 at 17:03
  • I've used the $SitecoreContextItem variable which works perfectly ! thank you for your help – Slrg Mar 21 at 17:52

Since version 4.0, Sitecore PowerShell Extensions includes a -WithParent parameter to the Get-ChildItem command. This includes the item itself, thus making this much simpler:

Get-ChildItem -Recurse . -WithParent |
  Where-Object { $_.Fields["__Enable item fallback"] -ne $null } |
  ForEach-Object {
    $_.Editing.BeginEdit()
    $_.Fields["__Enable item fallback"].Value = "1";
    $_.Editing.EndEdit()
  }

Sources:

@chorpo hit the nail on the head, Get-ChildItem does not get current item, but the children underneath the current Item. You will need to do a Get-Item as well to get/update the values of the current item. You should be able to kill two birds with one stone by getting both children and current items and appending:

$path = "."
$root = Get-Item $path
$items = Get-ChildItem $path -Recurse
($items += $root) | Where-Object { $_.Fields["__Enable item fallback"] -ne $null } | ForEach-Object {
    $_.Editing.BeginEdit()
    $_.Fields["__Enable item fallback"].Value = "1";
    $_.Editing.EndEdit()
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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