3

I am trying to enable item level language fallback to all items in my website. The below code snippet is taken for editing an standard field and checking whether the field got edited or not.

$Lang = "en";
$path = "master:\content\Sites\TEST";
[int]$count1 = 0;
Get-ChildItem $path -recurse -Language $Lang | ForEach-Object {
$field = $_.Fields["__Enable Item Fallback"];
if ($field -ne $null) {
    $_.Editing.BeginEdit()
    $field.Value = "1";
if ($_.Editing.EndEdit()) {
    $count1++;
  }
 }
}
Write-Host $count1 "items modified" -f Red;

When I see console,Editing.EndEdit() always returns a boolean value.

Is that a correct way to use Editing.EndEdit() along with if?

2 Answers 2

3

Editing item fields

First of all, yes, the way you're using BeginEdit() and EndEdit() is okay. Checking the returned value of EndEdit() doesn't make much sense though. This method always returns true if at least one field of the item has been modified.

There is a better way to update fields. Sitecore PowerShell Extensions generate properties for all item fields. You use them like this:

$item.'Field Name With Spaces' = 'some value'
$item.AnotherField = 'some other value'

Internally, SPE will take care of putting the item in and out of the editing mode. So using these properties will make your code much more concise.

Here's how the code should look in your case:

Get-ChildItem $path -Recurse -Language $Lang | % {
    $_.'__Enable item fallback' = $true
}

Enabling item-level language fallback

The way you approached enabling language fallback on all items will work... until you create new items and forget to enable the fallback for those items. There are better approaches.

Enable language fallback on all templates

You can set the __Enable item fallback field on the Standard Values for all of your item templates:

$templates =
    Get-ChildItem 'master:/sitecore/templates/User Defined' -Recurse |
    ? { $_.TemplateId -eq '{AB86861A-6030-46C5-B394-E8F99E8B87DB}' }

foreach($template in $templates) {

    $standardValuesPath = "master:$($template.FullPath)/__Standard Values"

    if(Test-Path $standardValuesPath) {
        $standardValues = Get-Item $standardValuesPath
    } else {
        $standardValues = New-Item -Path $standardValuesPath -Type $template.ID
        $template.'__Standard values' = $standardValues.ID
    }

    $standardValues.'__Enable item fallback' = $true
}

This will ensure that language fallback is enabled both for existing items and for all new items you may create in the future.

Note that the script above will create a __Standard values item if it doesn't exist in a template.

Enable language fallback on the root template

If all of your content templates inherit from the same base template, it is enough to manually set the "Enable language fallback" checkbox on the Standard Values item of that template. This value will be inherited by Standard Values of all descendant templates, and hence, by all items of those templates.

2
  • Thanks Dmytro. Its really helpful for me as a beginner. Also, you have mentioned about setting fallback at the root template. Most of the time, i have faced issue in my environment that _Standard values changes doesn't reflect to already existing items. Dec 9, 2016 at 21:51
  • You are welcome, Kumar. Also, if you are facing issues with Standard Values, feel free to post a separate question about that. Dec 10, 2016 at 15:08
6

Perhaps I can help add some clarity in the use of BeginEdit and EndEdit while using Sitecore PowerShell Extensions.

In most situations you do not need to use either of those while editing an item field through SPE.

Example: The following example demonstrates how to list out all of the available properties for an item. Methods and properties have been excluded from the example for clarity.

Get-Item -Path "master:\content\home" | Get-Member

   TypeName: Sitecore.Data.Items.Item

Name                                MemberType            Definition
----                                ----------            ----------
BeginEdit                           Method                void BeginEdit()
ChangeTemplate                      Method                void ChangeTemplate(Sitecore.Data.Items.TemplateItem template)
EndEdit                             Method                bool EndEdit()
ToString                            Method                string ToString()
PSChildName                         NoteProperty          string PSChildName=Home
PSDrive                             NoteProperty          PSDriveInfo PSDrive=master
PSIsContainer                       NoteProperty          bool PSIsContainer=True
PSParentPath                        NoteProperty          string PSParentPath=CmsItemProvider::master:\content
PSPath                              NoteProperty          string PSPath=CmsItemProvider::master:\content\Home
PSProvider                          NoteProperty          ProviderInfo PSProvider=CmsItemProvider
Item                                ParameterizedProperty string Item(string fieldName) {get;set;}, string Item(int index) {get;set;}, string Item(Sitecore.Data.ID fieldID) {get;set;}
ID                                  Property              Sitecore.Data.ID ID {get;}
Locking                             Property              Sitecore.Data.Locking.ItemLocking Locking {get;}
Name                                Property              string Name {get;set;}
Template                            Property              Sitecore.Data.Items.TemplateItem Template {get;}
TemplateID                          Property              Sitecore.Data.ID TemplateID {get;set;}
TemplateName                        Property              string TemplateName {get;}
ContentPath                         ScriptProperty        System.Object ContentPath {get=$this.Paths.ContentPath;}
FullPath                            ScriptProperty        System.Object FullPath {get=$this.Paths.FullPath;}
ItemPath                            ScriptProperty        System.Object ItemPath {get=$this.Paths.Path;}
MediaPath                           ScriptProperty        System.Object MediaPath {get=$this.Paths.MediaPath;}
ProviderPath                        ScriptProperty        System.Object ProviderPath {get=[Cognifide.PowerShell.Core.Utility.PathUtilities]::GetProviderPath($this);}
Text                                ScriptProperty        System.Object Text {get=$this["{A60ACD61-A6DB-4182-8329-C957982CEC74}"];set=[Cognifide.PowerShell.Core.Extensions.ItemShellExtensions]::Modify($this, "{A60ACD61-A6DB-4182-8329-C9...
Title                               ScriptProperty        System.Object Title {get=$this["{75577384-3C97-45DA-A847-81B00500E250}"];set=[Cognifide.PowerShell.Core.Extensions.ItemShellExtensions]::Modify($this, "{75577384-3C97-45DA-A847-8...
__Archive date                      ScriptProperty        System.Object __Archive date {get=[Sitecore.DateUtil]::IsoDateToDateTime($this["{56C15C6D-FD5A-40CA-BB37-64CEEC6A9BD5}"]);set=[Cognifide.PowerShell.Core.Extensions.ItemShellExten...
__Archive Version date              ScriptProperty        System.Object __Archive Version date {get=[Sitecore.DateUtil]::IsoDateToDateTime($this["{1D99005E-65CA-45CA-9D9A-FD7016E23F1E}"]);set=[Cognifide.PowerShell.Core.Extensions.ItemSh...
__Boost                             ScriptProperty        System.Object __Boost {get=$this["{93D1B217-B8F4-462E-BABF-68298C9CE667}"];set=[Cognifide.PowerShell.Core.Extensions.ItemShellExtensions]::Modify($this, "{93D1B217-B8F4-462E-BABF...
__Boosting Rules                    ScriptProperty        System.Object __Boosting Rules {get=$this["{8C181989-2794-4B28-8EE4-6BB5CB928DC2}"];set=[Cognifide.PowerShell.Core.Extensions.ItemShellExtensions]::Modify($this, "{8C181989-2794-...
__Bucket Parent Reference           ScriptProperty        System.Object __Bucket Parent Reference {get=$this["{9DAFCA1D-D618-4616-86B8-A8ACD6B28A63}"];set=[Cognifide.PowerShell.Core.Extensions.ItemShellExtensions]::Modify($this, "{9DAFC...
__Enable item fallback              ScriptProperty        System.Object __Enable item fallback {get=$this["{FD4E2050-186C-4375-8B99-E8A85DD7436E}"];set=[Cognifide.PowerShell.Core.Extensions.ItemShellExtensions]::Modify($this, "{FD4E2050...

As you can see above, ScriptProperty is a generated property on the object by SPE with a Getter/Setter.

Check out the book for more in-depth details on working with items.

Example: The following assigns an image to the Image field. SPE is smart enough to know that the logo is a MediaItem.

(Get-Item master:/content/home).Image = Get-Item 'master:\media library\logo'

Both examples do not require the use of BeginEdit and EndEdit.


Regarding the question you have for the boolean value returned with EndEdit: methods that return a value can be suppressed using Out-Null.

Example: The following does not use the generated properties provided by SPE; the item must therefore be in an editing mode before modification. Use the BulkUpdateContext to speed things up.

New-UsingBlock (New-Object Sitecore.Data.BulkUpdateContext) {
    foreach($item in Get-ChildItem -Path "master:\content\home") {
        $item.Editing.BeginEdit()
        $item["Title"] = "Sample Item"
        $item["Text"] = "Sample Item"
        $item.Editing.EndEdit() | Out-Null
    }
}

So in conclusion, consider the following:

If writing $item["FIELDNAME"] and $item.Fields["FIELDNAME"] use BeginEdit and EndEdit to place in editing mode; SPE does not automatically call them.

If writing $item."FIELDNAME" don't use BeginEdit and EndEdit because it's automatic and the Edit commands are called internally by SPE.

Try not to blend the syntax of the two above if possible. Favor the use of automatic variables as it makes the script easier to read. Performance may not come in to play unless editing many fields at a time, at which point the BulkUpdateContext should be considered.

In the example from Dmytro a property is generated for __Enable item fallback. When SPE tries to modify the value it internally detects that it's a boolean and sets a "1" or "0".

__Enable item fallback {
get=$this["{FD4E2050-186C-4375-8B99-E8A85DD7436E}"];
set=[Cognifide.PowerShell.Core.Extensions.ItemShellExtensions]::Modify($this, "{FD4E2050-186C-4375-8B99-E8A85DD7436E}", $Args );;
}
3
  • Thank you for an elaborate explanation! One question: is there any situation where it is better to use BeginEdit() and EndEdit() instead of the generated properties? When many fields are edited on every item, perhaps? P.S. The name is spelled "Dmytro" :) Dec 9, 2016 at 7:59
  • My apologies Dmytro, reminds me of how I feel when my name is spelled "Micheal". Dec 9, 2016 at 13:15
  • Thank you Michael :) . Its really nice elaboration on working with items in SPE. Dec 9, 2016 at 21:55

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.