Console icon indicating copy to clipboard operation
Console copied to clipboard

Add-Rendering throws an exception if trying to provide Parameter hashtable using Remoting

Open stanislavSV opened this issue 5 months ago • 1 comments

"Add-Rendering" command fails if "Parameter" parameter is provided in case if run a script using remote session. The same script (I mean "Add-Rendering" part itself) works OK in case if run it in Sitecore PowerShell ISE directly.

Steps to reproduce: Try to add a rendering to an item using the following remote script in Windows PowerShell:

Import-Module -Name SPE

$session = New-ScriptSession -Username {USER_NAME} -Password {USER_PASSWORD} -ConnectionUri https://{YOUR_SITECORE_HOSTNAME}/

Invoke-RemoteScript -Session $session -ScriptBlock { $rendering = Get-Item -Path "master:/sitecore/layout/Renderings/Sample/Sample Rendering" | New-Rendering; Add-Rendering -Path "{ITEM_PATH}" -Instance $rendering -DataSource "datasource" -Placeholder "main" -FinalLayout -Parameter @{ "sample" = "value" }; }

Stop-ScriptSession -Session $session

Expected result: The rendering is added successfully.

Actual result: The error below appears:

Add-Rendering : The Sitecore.Client.ContentDatabase is null. At line:3 char:1 + Add-Rendering -Path "master:/sitecore/content/Home/Tests/Presentation ... + CategoryInfo : NotSpecified: (:) [Add-Rendering], InvalidOperationException + FullyQualifiedErrorId : System.InvalidOperationException,Spe.Commands.Presentation.AddRenderingCommand

Note: It works OK if remove "Parameter" parameter from the "Add-Rendering" command.

Version: Spe, Version=7.0.0.0 Hotfix SXA-8155-1

Docker Image: scr.sitecore.com/xmcloud/sitecore-xmcloud-cm:1-ltsc2019

stanislavSV avatar Jun 23 '25 07:06 stanislavSV

Looks like it could be breaking on this line:

var renderingItem = Sitecore.Client.ContentDatabase.GetItem(Instance.ItemID);

michaellwest avatar Jun 23 '25 15:06 michaellwest

@michaellwest Will Sitecore.Client.ContentDatabase be always null for SPE remoting? We have item in this context. Is it ok to get the database from the item, like if Sitecore.Client.ContentDatabase is null?

Antonytm avatar Jun 23 '25 20:06 Antonytm

Implemented a fix at the following PR: https://github.com/SitecorePowerShell/Console/pull/1378

stanislavSV avatar Jun 25 '25 16:06 stanislavSV

Great catch! Thank you for the fix!

AdamNaj avatar Aug 07 '25 15:08 AdamNaj