Speed up content update with Sitecore PowerShell Extensions module

Dominik Markowski
3 min readNov 2, 2020

Sitecore PowerShell Extensions module (SPE) provides a command line and environment to run scripts in PowerShell query language. SPE is commonly used to automate content update, perform statistical analysis and many more. Using SPE can speed up the entire development process by reduction of manual steps across testing, maintaining environments and deployment. It also eliminates mistakes caused by miscommunication or human factor.

SPE has great documentation hosted on gitbook with many useful examples. I strongly recommend it. If you looking for ready-to-use code snippets this article is for you. In the following paragraphs, I present the few most common content update tasks that developers might want to automate.

Each task is described in the format:
<Title>
<Story description>
<Code snippet with solution>

Create item

I want to create an item called New Content Page, based on a given template and placed under the item \content\Home.

$newItemPath = "master:\content\Home"
$newItemName = "New Content Page"
$newItemTemplateId = "{D181D669-5B65-4E91-BB17-24645798B14B}"
New-Item -Path $newItemPath -Name $newItemName -ItemType $newItemTemplateId

Modify item

I want to set a value of the field Description in the item identified by its path \content\Home\New Content Page.

$itemPath = "master:\content\Home\First Item"
$fieldName = "Description"
Function Custom-EditItem {
Param ($item, $fieldName, $fieldValue)
$item.Editing.BeginEdit()
$item[$fieldName] = $fieldValue
$item.Editing.EndEdit()
}
$item = Get-Item -Path $itemPath# get item by path
Custom-EditItem $item $fieldName "Test description"

Delete item

The item \content\Home\Old Item is obsolete, so I want to delete it.

$oldItemPath = "master:\content\Home\Old Item"Get-Item -Path $oldItemPath | Remove-Item

Add rendering

I want to add a new rendering to the item content\Home\Content Page. The rendering is defined by its ID.

$pageItem = Get-Item -Path "master:\content\Home\Content Page"
$renderingId = "{6C587775-545C-4FB0-9258-4454F361124E}"
$renderingPlaceholder = "main"
$renderingItem = Get-Item -Path "master" -ID $renderingId | New-Rendering -Placeholder $renderingPlaceholderAdd-Rendering -Item $pageItem -PlaceHolder $renderingPlaceholder -Instance $renderingItem

Set rendering data-source

I want to set a data-source for rendering. The rendering is described by its ID and placeholder. The data-source item is defined by its ID.

$pageItem = Get-Item -Path "master:\content\Home\Content Page"
$renderingId = "{6C587775-545C-4FB0-9258-4454F361124E}"
$renderingPlaceholder = "main"
$datasourceId = "{A6350C33-C716-45B9-9AA0-83EFBD053609}"
Get-Rendering -Item $pageItem -PlaceHolder $renderingPlaceholder
| Where-Object {$_.ItemID -eq $renderingId}
| Foreach-Object {
$_.DataSource = $datasourceId
Set-Rendering -Item $pageItem -Instance $_
}

Set rendering parameter

I want to set a value of the rendering parameter Param1 to true. The rendering is described by its ID and placeholder.

$parameters = [ordered]@{"Display Subtitle"="1"}
$pageItem = Get-Item -Path "master:\content\Home\Content Page"
$renderingId = "{6C587775-545C-4FB0-9258-4454F361124E}"
$renderingPlaceholder = "main"
Get-Rendering -Item $pageItem -PlaceHolder $renderingPlaceholder
| Where-Object {$_.ItemID -eq $renderingId}
| Set-RenderingParameter -Parameter $parameters
| Set-Rendering -Item $pageItem

Set rendering placeholder

I want to change a placeholder of the rendering from main to footer. The rendering is identified by its ID and placeholder.

$pageItem = Get-Item -Path "master:\content\Home\Content Page"
$renderingId = "{6C587775-545C-4FB0-9258-4454F361124E}"
$renderingPlaceholder = "main"
$newRenderingPlaceholder = "footer"
Get-Rendering -Item $pageItem -PlaceHolder $renderingPlaceholder
| Where-Object {$_.ItemID -eq $renderingId}
| Foreach-Object {
$_.Placeholder = $newRenderingPlaceholder
Set-Rendering -Item $pageItem -Instance $_
}

Start using SPE and make your life with Sitecore easier.

My experience shows that in about 90% of content update tasks are to create/edit/delete a specific item or modify renderings data-source/parameter/placeholder. So now, you are ready to go and start using SPE for automated content update. You can download code snippets and package with useful function from my GitLab repo.

Useful links

SPE documentation
SPE repository
SPE Sitecore marketplace

Shout-out to SPE dev team:

Adam Najmanowicz | adamnaj
Michael West | michaelwest101

--

--

Dominik Markowski
0 Followers

Dominik is a software engineer with background in web development and space engineering. Currently working with Sitecore and ASP.NET technologies.