Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scjunkie/6dc0a3b12f8b121ef8fc to your computer and use it in GitHub Desktop.
Save scjunkie/6dc0a3b12f8b121ef8fc to your computer and use it in GitHub Desktop.
Cut Down Development Time with SPE PowerShell Scripts
#=============================================================
#Verbs and commands
#=============================================================
Get-Verb
Get-Command -Verb Get
#=============================================================
#Create Items
#=============================================================
$item = New-Item . –type "\templates\sample\sample item" –Name "Mike Test"
$item |% { $_.Title = “This is my awesome title!”; $_.Text = "<h1>Hi!</h1>" }
$image = Get-Item "/sitecore/media library/Images/Cats/Wigs for Cats10"
$item.Editing.BeginEdit()
$imageField = [Sitecore.Data.Fields.ImageField]$item.Fields["Image"]
$imageField.MediaID = $image.ID
$item.Editing.EndEdit()
#=============================================================
#Delete Items
#=============================================================
Remove-Item "/sitecore/content/home/mike test"
#=============================================================
#Item Cloning
#=============================================================
$item = Get-Item("/sitecore/content/Home/Page Five")
$destination = Get-Item("/sitecore/content/Clones")
New-ItemClone -Item $item -Destination $destination -Recursive
#=============================================================
#Serialize content tree
#=============================================================
Get-Item . | Serialize-Item -Recurse
#=============================================================
#Expand tokens (bulk item update)
#=============================================================
@(Get-Item .) + (Get-ChildItem -r .) | ForEach-Object { Expand-Token $_ }
#=============================================================
#Deserialize content tree
#=============================================================
Deserialize-Item -Path "D:\inetpub\wwwroot\spe\Data\serialization\master\sitecore\content" -Root "D:\inetpub\wwwroot\spe\Data\serialization\" -Recurse -ForceUpdate z
#=============================================================
#Item Context Menu
#=============================================================
<#
.NAME
Expand Tokens
.SYNOPSIS
Expand tokens in all fields for the current Item
.NOTES
Mike Reynolds
#>
$item = Get-Item .
$tokenReplacer = [Sitecore.Configuration.Factory]::GetMasterVariablesReplacer()
$item.Editing.BeginEdit()
$tokenReplacer.ReplaceItem($item)
$item.Editing.EndEdit()
Close-Window
#=============================================================
#Custom Report
#=============================================================
<#
.NAME
Items with unexpanded tokens in fields
.SYNOPSIS
Report of all Content Items with unexpanded tokens in fields
.NOTES
Mike Reynolds
#>
function Get-ItemsWithUnexpandedTokens {
return Get-ChildItem -Path "master:\sitecore\content" -Recurse | Where-Object { Has-UnexpandedTokens($_) }
}
function Has-UnexpandedTokens($item) {
$tokens = @("$name", "$id", "$parentid", "$parentname", "$date", "$time", "$now")
$item.Fields.ReadAll()
foreach($field in $item.Fields) {
foreach($token in $tokens) {
if($field.Value.Contains($token)) {
return $true
}
}
}
return $false;
}
$items = Get-ItemsWithUnexpandedTokens
if($items.Count -gt 0) {
$props = @{
InfoTitle = "Content Items with unexpanded tokens in fields."
InfoDescription = "Lists all content Items with unexpanded tokens in fields."
PageSize = 25
}
$items |
Show-ListView @props -Property @{Label="Name"; Expression={$_.DisplayName} },
@{Label="Updated"; Expression={$_.__Updated} },
@{Label="Updated by"; Expression={$_."__Updated by"} },
@{Label="Created"; Expression={$_.__Created} },
@{Label="Created by"; Expression={$_."__Created by"} },
@{Label="Path"; Expression={$_.ItemPath} }
}
else {
Show-Alert "There are no Items with unexpanded tokens."
}
Close-Window
#=============================================================
#Toolbox tool
#=============================================================
<#
.NAME
Expand tokens in all content items
.SYNOPSIS
Expand tokens in all fields in all content items
.NOTES
Mike Reynolds
#>
Get-ChildItem -Path "master:\sitecore\content" -Recurse | ForEach-Object { Expand-Token $_ }
Close-Window
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment