Sitecore Powershell Extension (SPE)
Introduction
SPE (Sitecore Powershell Extension) is very useful tool/module developed by Adam Najmanowicz and Michael West that provides a command line and scripting environment for automating tasks. Sitecore Powershell Extensions works with Sitecore process, which can make native calls to Sitecore APIs and allows to change/update the Sitecore Items on the fly. The same Windows PowerShell syntax is used for running commands and writing scripts.
Installation
Go to sitecore marketplace and download it if you are certified developer Otherwise please contact any certified developer to download the desired version for you.
URL: https://marketplace.sitecore.net/en/Modules/S/Sitecore_PowerShell_console.aspx
Where and why use Sitecore Powershell Scripting
- Create multiple new items without any manual activity
- Getting Sitecore Item details in single click
- Get Item by path
- Get Items from all languages and versions.
- Deleting Items based on specific conditions
- Data Import
- Bulk Data Update
- Reporting
- Creating Sitecore Package
- Publishing Sitecore Items.
- Several other features.
Create Sitecore Item
$itemPath = "master:/sitecore/content/Global Content/People/A/AaAd/TestPerson"
$templateId = "{EB65BFA3-5122-43F4-A1B0-02F430F645CA}"
New-Item -Path $itemPath -ItemType $templateId
Get Sitecore Item Details
$rootItemId = "{8B20A7D5-B236-43A6-91BC-541256B0B55B}"
$rootItem = Get-Item -Path "master:" -ID $rootItemId
$templateId = "{D55E796B-7E58-4F88-9F28-CA3CD388BE43}"
$query = "fast:$($rootItem.ItemPath)//*[@@templateid='$($templateId)']"
$item = Get-Item -Path "master:" -Query $query
Write-Host $item.Name
Get Sitecore Item Details By Path
Get-Item -Path "master:/sitecore/content/Home/Home/Global Presence/Africa"
-Language en |
Format-Table -Property DisplayName, Language, Id, Version,TemplateName
Get Sitecore Item Details from all languages and versions
All Languages
Get-Item -Path "master:/sitecore/content/Home/Home/Global Presence/Africa"-Language * |
Format-Table -Property DisplayName, Language, Id, Version,TemplateName
All Versions
Get-Item -Path "master:/sitecore/content/Home/Home/Global Presence/Africa"-Language en -Version * |
Format-Table -Property DisplayName, Language, Id,Version, TemplateName
Delete items
Remove-ItemVersion -Path master:/sitecore/content/GlobalContent/People/A/Aa Ad/test -Language * -Recurse
Data Import
In most cases while migrating site from other platform/cms to sitecore, weneed to import data from the existing datasource. Sitecore Powershell has thepowerful feature to read the external files like csv which help bulk data importto the Sitecore.
function RunQuery {
$resultSet = Import-Csv "D:/Desktop/Update URL Key/Consolidate-2018-10-30_070121.csv";
foreach ($row in $resultset)
{
$targetItemID = $row.ID;
$UrlKey = $row.UrlKey;
Write-Host "Item ID : $targetItemID , Url Key : $UrlKey";
}
}
RunQuery {}
Bulk Update
Using powershell script you can update lots of sitecore items in few minutes. Below is the code snippet to update the item.
function RunQuery {
$resultSet = Import-Csv "D:/Desktop/Update URL Key/Consolidate-2018-10-30_070121.csv";
foreach ($row in $resultset)
{
$targetItemID = $row.ID;
$UrlKey = $row.UrlKey;
$targetItem = Get-Item -Path master: -Id "$targetItemID";
$targetItem.Editing.BeginEdit();
$targetItem.fields["UrlKey"].Value="$UrlKey";
$targetItem.Editing.EndEdit() | Out-Null;
Write-Host "Item ID : $targetItemID , Url Key : $UrlKey";
}
}
RunQuery {}
Reporting / Data Export
Another powerful feature is to export data or report in csv, excel, html, excelformat. To explain it let’s execute the below code snippet
Get-ChildItem "master:/sitecore/content/Home/Home/Find YourDentons Team" -Language "en" -Recurse |
Where-Object { $_.TemplateName -eq "Practice Page"} |
Show-ListView -Property ID, Name, ItemPath
Sitecore Package
$ItemsToBeDeployed = @(@{ Source = "master:/content/.../.../..." },);
$ErrorActionPreference = "Stop";
$Package = New-Package -Name "My Package";
$Package.Sources.Clear();
$Package.Metadata.Author = "PowerShell";
$Package.Metadata.Publisher = "Altudo";
$Package.Metadata.Version = Get-Date -Format FileDateTimeUniversal;
$Package.Metadata.Readme = 'This will install a Sitecore Package generated using PowerShell';
ForEach ($Item in $ItemsToBeDeployed)
{
$Source = Get-Item $Item.Source | New-ItemSource -Name "N/A" -InstallMode Overwrite;
$Package.Sources.Add($Source);
}
# Save and Download Package
Export-Package -Project $package -Path "$( $package.Name ) - $( $package.Metadata.Version ).zip" -Zip;
Download-File "$SitecorePackageFolder\$( $package.Name ) - $( $package.Metadata.Version ).zip";
Publish Items
$Sitecore_Item = Get-Item -Path master: -Id"{E18B99CD-6C6C-40F5-86DB-4B82F33AF991}" -language "en";
$Sitecore_ItemPath = $Sitecore_Item.paths.fullpath;
Get-Item $Sitecore_ItemPath | Publish-Item -Recurse -Target web -language"en" -PublishMode Full -RepublishAll;
Other features
- Save and share script file.
- Debug scripts
- Import in csv, xml, html, excel format
- Selected script execution
- etc....
Disadvantages of Sitecore Powershell
No ROLLBACK
Please do let me know if you have any questions or discussion points. Comment please if you have any feedback.
Comments
Post a Comment