Creating Custom Interactive Dialog to execute Sitecore PowerShell Script

Hello everyone, we will try to learn how we can create a interactive dialog in Sitecore to execute powershell script. In this article we will see how we can convert a view rendering or controller rendering to JSON rendering via powershell interactive dialog. We will also check that how we will be adding the button to Ribbon in Sitecore.

We will divide the blog into 3 section.

Step 1: Navigate to Script Library under PowerShell under Modules. Path : /sitecore/system/Modules/PowerShell/Script Library.

Step 2: Right click Script Library --> Insert --> PowerShell Script Module --> Give A Name(CustomPowershell2) --> Ok

Step 3: Right Powershell Script Module which is created --> Insert --> PowerShell Script Library --> Give A Name(Content Editor) --> Ok

Step 4: Now create the content tree structure which is shown using PowerShell Script Library

Step 5: Navigate to CustomViewCommand and Right Click --> Insert --> PowerShell Script --> Create dialog will open(Add a name to your command like ConvertToJsonRendering) --> Ok

The script field will let you write the custom powershell script code.

Step 6: Now we will add the configuration in Core DB. Open to Core Database --> Navigate to path /sitecore/content/Applications/Content Editor/Ribbons/Strips/View --> Add new Chunk(CustomViewCommand) --> Add new panel(ConvertToJsonRendering) --> Add new Large Button(ConvertToJsonRendering).

The Highlighted Item id will be your script item id created in Master Database. Basically it will execute that powershell script.
When you open Master Database, now you can see the button avaialable inside the View tab in Sitecore Ribbon.

Now its time write the PowerShell Script. I will add the script here and we will understand the script wherever its necessary

       
$global:finalitemid = $null
$options = @{
    "Controller"="Controller"
    "View"="View"
}

$props = @{
    Parameters = @(
        @{Name="selectedOption"; Title="Select Rendering"; Options=$options; Tooltip="Renderings"}
    )
    Title = "Rendering selector"
    Description = "Choose the option."
    Width = 600
    Height = 400
    OkButtonName = "Proceed"
    CancelButtonName = "Abort"
    ShowHints = $true
}

$result = Read-Variable @props

if ($result -ne "ok") {
    Exit
}
$selectedRendering = $selectedOption

$conrollerRenderingOptions = @{}
$sxaPath = "/sitecore/layout/Renderings/Feature"
if ($selectedRendering -eq "Controller")
{
	$items = @(Get-ChildItem -Path $sxaPath -Recurse | Where-Object -Property TemplateId -eq -Value '{2A3E91A0-7987-44B5-AB34-35C2D9DE83B9}')
	if($items.Count -le  1)
	{
		Exit
	}
    foreach($item in $items)
		{
			$conrollerRenderingOptions[$item.Name] = $item.Id	
		}
}else{
	$items = @(Get-ChildItem -Path $sxaPath -Recurse | Where-Object -Property TemplateId -eq -Value '{99F8905D-4A87-4EB8-9F8B-A9BEBFB3ADD6}')
	if($items.Count -le  1)
	{
		Exit
	}
	foreach($item in $items)
	{
				$conrollerRenderingOptions[$item.Name] = $item.Id
	}
}

$RenderingProps = @{
    Parameters = @(
		@{ Name = "RenderingId"; Title = "Selected Rendering "+$selectedRendering; Options=$conrollerRenderingOptions; Tab = "Renderings" }
    )
    Title = $selectedRendering
	Description = "Selected Renderings Types"
    OkButtonName = "Proceed"
    CancelButtonName = "Abort"
	Width = 600
	Height = 400
}

$result = Read-Variable @RenderingProps

if ($result -ne "ok") {
    Exit
}
$selectedRenderingId = $RenderingId
$currentItem = (Get-Item $selectedRenderingId ).Name 
$Convertoptions = @{
    "JSONRendering"="JSONRendering|"+$selectedRenderingId
    "Duplicate"="Duplicate|"+$selectedRenderingId
}
$RenderingPropsItem = @{
    Parameters = @(
		@{ Name = "Convertername"; Title = "Selected Rendering "+$currentItem; Options=$Convertoptions; Tab = "Current Selected Renderings" }
    )
    Title = $currentItem
	Description = "Selected Renderings Item"
    OkButtonName = "Proceed"
    CancelButtonName = "Abort"
	Width = 600
	Height = 400
}

$result = Read-Variable @RenderingPropsItem

if ($result -ne "ok") {
    Exit
}
$Convertertype = $Convertername.Split("|");
Write-Host "ConverterName: " $Convertertype[0];
Write-Host "Item id: " $Convertertype[1];
$global:finalitemid = $Convertertype[1];
if ($Convertertype[0] -eq "JSONRendering")
{
	$item = (Get-Item $Convertertype[1]);
	$itemname = $item.Name;
	Write-Host "Item Name: " $item.Name
	$item.Editing.BeginEdit();
            $item.TemplateId = "{04646A89-996F-4EE7-878A-FFDBF1F0EF0D}";
			$item["Component Name"] = $itemname;
	$item.Editing.EndEdit();
	Write-Host "Item id: " $item.ID " - Template id: " $item.TemplateId "- Template name: " $item.Name
	Show-Confirm -Title "Successfully changed the current rendering to JSON Rendering"
}else{
    Write-Host "Item id: " $global:finalitemid;
	$RenderingPropsItemPathDestination = @{
    Parameters = @(
		@{ Name = "DestionationItemPath"; Title = "Destination Folder to Create Item"; Tab = "Destination Folder" }
    )
    Title = Select Path
	Description = "Selected Renderings Path where New Item Should be created"
    OkButtonName = "Proceed"
    CancelButtonName = "Abort"
	Width = 600
	Height = 400
}
$result = Read-Variable @RenderingPropsItemPathDestination
if ($result -ne "ok") {
    Exit
}
$ItemDestination = $DestionationItemPath
Write-Host "Item path: " $ItemDestination;
$lastitem = $global:finalitemid;
$finallitem = (Get-Item $lastitem);
$ItemDestination = $ItemDestination + '/'
$newitem = New-Item -Path $ItemDestination -Name $finallitem.Name -ItemType "/sitecore/templates/Foundation/JavaScript Services/Json Rendering"
$newitem.Editing.BeginEdit()
$newitem["Component Name"] = $finallitem.Controller
$newitem.Editing.EndEdit()
Write-Host "Item created: " $newitem.Name
Show-Confirm -Title "Successfully duplicated SXA rendering to JSON Rendering on mentioned path"
}
	
 
Let's try to undertstand the script
       
$global:finalitemid = $null
$options = @{
    "Controller"="Controller"
    "View"="View"
}

$props = @{
    Parameters = @(
        @{Name="selectedOption"; Title="Select Rendering"; Options=$options; Tooltip="Renderings"}
    )
    Title = "Rendering selector"
    Description = "Choose the option."
    Width = 600
    Height = 400
    OkButtonName = "Proceed"
    CancelButtonName = "Abort"
    ShowHints = $true
}

$result = Read-Variable @props

if ($result -ne "ok") {
    Exit
}
$selectedRendering = $selectedOption
	   
 

This script block has option list which have two values, controller and view. The next code script is for creating interactive dialog which takes the option selected by user and pass it to next interactive dialog coming in next step. $selectRendering will hold the value of selected option.

       
$conrollerRenderingOptions = @{}
$sxaPath = "/sitecore/layout/Renderings/Feature"
if ($selectedRendering -eq "Controller")
{
	$items = @(Get-ChildItem -Path $sxaPath -Recurse | Where-Object -Property TemplateId -eq -Value '{2A3E91A0-7987-44B5-AB34-35C2D9DE83B9}')
	if($items.Count -le  1)
	{
		Exit
	}
    foreach($item in $items)
		{
			$conrollerRenderingOptions[$item.Name] = $item.Id	
		}
}else{
	$items = @(Get-ChildItem -Path $sxaPath -Recurse | Where-Object -Property TemplateId -eq -Value '{99F8905D-4A87-4EB8-9F8B-A9BEBFB3ADD6}')
	if($items.Count -le  1)
	{
		Exit
	}
	foreach($item in $items)
	{
				$conrollerRenderingOptions[$item.Name] = $item.Id
	}
}
	   
 

This script block has check the selected option and accordingly filter out the renderings as add them as option list for next interactive dialog

       
    $RenderingProps = @{
    Parameters = @(
		@{ Name = "RenderingId"; Title = "Selected Rendering "+$selectedRendering; Options=$conrollerRenderingOptions; Tab = "Renderings" }
    )
    Title = $selectedRendering
	Description = "Selected Renderings Types"
    OkButtonName = "Proceed"
    CancelButtonName = "Abort"
	Width = 600
	Height = 400
}

$result = Read-Variable @RenderingProps

if ($result -ne "ok") {
    Exit
}
$selectedRenderingId = $RenderingId
$currentItem = (Get-Item $selectedRenderingId ).Name 
$Convertoptions = @{
    "JSONRendering"="JSONRendering|"+$selectedRenderingId
    "Duplicate"="Duplicate|"+$selectedRenderingId
}
$RenderingPropsItem = @{
    Parameters = @(
		@{ Name = "Convertername"; Title = "Selected Rendering "+$currentItem; Options=$Convertoptions; Tab = "Current Selected Renderings" }
    )
    Title = $currentItem
	Description = "Selected Renderings Item"
    OkButtonName = "Proceed"
    CancelButtonName = "Abort"
	Width = 600
	Height = 400
}

$result = Read-Variable @RenderingPropsItem

if ($result -ne "ok") {
    Exit
}
$Convertertype = $Convertername.Split("|");
Write-Host "ConverterName: " $Convertertype[0];
Write-Host "Item id: " $Convertertype[1];
$global:finalitemid = $Convertertype[1];
if ($Convertertype[0] -eq "JSONRendering")
{
	$item = (Get-Item $Convertertype[1]);
	$itemname = $item.Name;
	Write-Host "Item Name: " $item.Name
	$item.Editing.BeginEdit();
            $item.TemplateId = "{04646A89-996F-4EE7-878A-FFDBF1F0EF0D}";
			$item["Component Name"] = $itemname;
	$item.Editing.EndEdit();
	Write-Host "Item id: " $item.ID " - Template id: " $item.TemplateId "- Template name: " $item.Name
	Show-Confirm -Title "Successfully changed the current rendering to JSON Rendering"
}else{
    Write-Host "Item id: " $global:finalitemid;
	$RenderingPropsItemPathDestination = @{
    Parameters = @(
		@{ Name = "DestionationItemPath"; Title = "Destination Folder to Create Item"; Tab = "Destination Folder" }
    )
    Title = Select Path
	Description = "Selected Renderings Path where New Item Should be created"
    OkButtonName = "Proceed"
    CancelButtonName = "Abort"
	Width = 600
	Height = 400
}
$result = Read-Variable @RenderingPropsItemPathDestination
if ($result -ne "ok") {
    Exit
}
$ItemDestination = $DestionationItemPath
Write-Host "Item path: " $ItemDestination;
$lastitem = $global:finalitemid;
$finallitem = (Get-Item $lastitem);
$ItemDestination = $ItemDestination + '/'
$newitem = New-Item -Path $ItemDestination -Name $finallitem.Name -ItemType "/sitecore/templates/Foundation/JavaScript Services/Json Rendering"
$newitem.Editing.BeginEdit()
$newitem["Component Name"] = $finallitem.Controller
$newitem.Editing.EndEdit()
Write-Host "Item created: " $newitem.Name
Show-Confirm -Title "Successfully duplicated SXA rendering to JSON Rendering on mentioned path"
}
	   
 

This script will execute the interactive dialog with selected rendering id and gives the option of duplicate the item in specified path by converting it to JSON rendering and comvert the template of existing item JSON rendering.

Convert To JSON rendering

Duplicate To JSON rendering in Specified Path

Hope in this article we were able to learn creating interactive dialog in Sitecore and how we can pass one parameter to another interactive dialog and execute the script.

Happy learning. Happy Sitecoring!!

You can find my other Sitecore Related blogs on Here

Comments

Popular posts from this blog

Sitecore XM Cloud Form Integration with Azure Function as Webhook

Automate RSS Feed to Sitecore XM Cloud: Logic App, Next.js API & Authoring API Integration

Create and Fetch Content From Sitecore Content Hub One using GraphQL and React