Install Items to custom location using SXA Scaffolding Scripts

Scaffolding in Sitecore lets you add modules/components to tenants and sites. OOTB SXA modules are stored in Feature or Foundation folder:

  • /sitecore/system/Settings/Feature/Experience Accelerator
  • /sitecore/system/Settings/Foundation/Experience Accelerator

In addition to that we can create our own custom SXA components/modules which can be installed just like regular SXA modules.

The SXA components we build could have some site items which we want to install to specific location(s) when this component gets installed like specific data/settings/dictionary/under home etc… This is where SXA scaffolding comes into picture- it enables module-items to install into custom location(s) based on the component requirements so that content authors doesn’t have to install it manually and we give them a seamless module installation experience.

Scaffolding can be configured on tenant or site level, you can see scaffolding options when you create a module, below screen shot for ref.

We have OOTB options to create items to some default location(s) like:

  • Under home item
  • Under Data
  • Some standard items under Presentation and Settings.

For e.g. we can create item of type “AddItem” and specify the location and template, to define where and what type of item we want to create, this helps in installing item(s) in some standard location(s) supported by SXA.

How about if we want to install some item(s) into such location(s) which doesn’t support OOTB by scaffolding action items?

Problem statement : We want to create dictionary items under Site-> Site Dictionary->[Component Dictionary Item] while installing a new module to specific site.

So, how we go there? – we can leverage ExecuteScript action item and write script to create item(s) in any specific location, here are the steps to follow:

  1. Navigate to your site setup item.
  2. Right click on site setup item and create an item say [AddDictionary] based on “ExecuteScript” template.
  3. The script which we are going to create will be referred in the field called “Script” under section “Script
  4. Navigate to /sitecore/system/Modules/PowerShell/Script Library and Create an item say. [TestModule] of type “PowerShell Script Module” template.
  5. Right click on [TestModule] item and create an item say [Functions] based on “PowerShell Script Library” template
  6. Right click on [Functions] and create following scripts:

[Add-SXAModuleDictionary]

This script is used to create dictionary item under site->site dictionary domain> for selected site.

Import-Function Get-DictionaryDomain
function Invoke-ModuleScriptBody {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0 )]
[Item]$Site,
[Parameter(Mandatory = $true, Position = 1 )]
[Item[]]$TenantTemplates
)
begin {
Write-Verbose "Cmdlet Invoke-ModuleScriptBody – Begin"
}
process {
Write-Verbose "Cmdlet Invoke-ModuleScriptBody – Process"
$dictionaryDomain = Get-DictionaryDomain $Site
$dictionaryDomainPath=$dictionaryDomain.Paths.Path
$dictionaryDomainItem= Get-Item -Path $dictionaryDomainPath
# dictionary branch template Id…
$templateId = "{BC8C810C-ACAC-4C40-9A37-036BC6B2B93D}"
New-Item -Parent $dictionaryDomainItem -Name "TestModuleDictionary" -ItemType $templateId
}
end {
Write-Verbose "Cmdlet Invoke-ModuleScriptBody – End"
}
}

[Get-DictionaryDomain]

This script returns the dictionary domain item based on selected site node and called from Add-SXAModuleDictionary script.

function Get-DictionaryDomain {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, ValueFromPipeline = $true, Position=0 )]
[Item]$Root
)
begin {
Import-Function Test-ItemIsDictionaryDomain
Write-Verbose "Cmdlet Get-DictionaryDomain – Begin"
}
process {
Write-Verbose "Cmdlet Get-DictionaryDomain – Process"
Get-ChildItem -Path $Root.Paths.Path -Recurse | ? { (Test-ItemIsDictionaryDomain $_ ) -eq $true } | Select-Object -First 1
}
end {
Write-Verbose "Cmdlet Get-DictionaryDomain – End"
}
}

[Test-ItemIsDictionaryDomain]

This script filters the dictionary domain item based on items from selected site node and called from Get-DictionaryDomain script.

function Test-ItemIsDictionaryDomain {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, ValueFromPipeline = $true, Position=0 )]
[Item]$Item
)
begin {
Write-Verbose "Cmdlet Test-ItemIsDictionaryDomain – Begin"
}
process {
Write-Verbose "Cmdlet Test-ItemIsDictionaryDomain – Process"
# dictionary domain template Id…
[Sitecore.Data.ID]$DataSourceConfigurationsTemplateID = "{0A2847E6-9885-450B-B61E-F9E6528480EF}"
[Sitecore.Data.Managers.TemplateManager]::GetTemplate($Item).InheritsFrom($DataSourceConfigurationsTemplateID)
}
end {
Write-Verbose "Cmdlet Test-ItemIsDictionaryDomain – End"
}
}

Once you have all the scripts created, navigate back to your site setup scaffolding and click on [AddDictionary] item which you created above in step#2 and set[ Add-SXAModuleDictionary ] script to the Script field.

Next- try to install the module using script->Add site module and select your related module and after the script execution is over you can go back to your Site-> Site dictionary domain-> and you should see [TestModuleDictionary] item created there.

This way you can perform any kind of operation while you are installing specific module to a site, this was just an example to show how you can leverage this feature to provide a seamless module installation experience to your authors.

Hope it helps!

Happy learning 🙂

Reference(s):

https://doc.sitecore.com/developers/sxa/17/sitecore-experience-accelerator/en/add-modules-to-site-and-tenant-scaffolding.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s