1

I would like to get SXA Site name dynamically when we run PowerShell script through the context menu. Currently I am giving static Site name in PowerShell script. Using the below code.

Show-Input "Enter Site name"

I am using multiple sites so need it dynamically.

3 Answers 3

4

There is an out of the box SXA function called Get-SiteItem that takes the item as parameter and gives the context site name. Using Get-Item function you can get the context item by using . as parameter.

Import-Function Get-SiteItem
$item = Get-Item . // Gets context item
$site =  Get-SiteItem $item // Gets context site item for the current item
Write-host $site.Name 
0

You can run this command to get all your site names in your Sitecore Instance and then use a filter to get the specific one.

$siteNames = [Sitecore.Sites.SiteManager]::GetSites() | % {$_.Name}
Write-Host $siteNames | Select-Object -Unique

Hope this helps.

5
  • This code is showing all the sites name. I would like to get only selected item Sitename through Powershell script .
    – Nisha
    May 5, 2022 at 6:42
  • Okay, can you give some more context like how you will get the selected item Sitename? Do you mean you will provide the Site item name or what? Please mention. May 5, 2022 at 6:46
  • I am running Powershell script through context menu. We have multiple sites and every site use this script and we would like to get Sitename dynamically instead of giving it static.
    – Nisha
    May 5, 2022 at 6:54
  • Can you provide some code snippets where you are defining it as static? May 5, 2022 at 6:55
  • Updated question.
    – Nisha
    May 5, 2022 at 7:08
0

You can use the below code to get all the sites in a list.

The list can be used in a dropdown for the user to select the site.

$SiteResolver = [System.Type]([Sitecore.XA.Foundation.Multisite.ISiteInfoResolver])
$Sites = [Sitecore.DependencyInjection.ServiceLocator]::get_ServiceProvider().GetService($SiteResolver).Sites`
| ForEach-Object { $database.GetItem($_.RootPath) }`
| Where-Object { $_ -ne $null -and $_.ID -ne $null -and $_.TemplateId -eq [Constants]::SiteTemplateId} `
| Sort-Object -Unique

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.