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 );;
}