Mark Gibbons
Published on

Personalisation error in CM on ‘Hide’ condition

Authors

If you’re getting the following error in Experience Editor on personalised components, then read on for a solution.

Error Rendering Xslt: /xsl/system/webedit/hidden rendering.xslt: The ‘ ‘ character, hexadecimal value 0x20, cannot be included in a name.

This happens when the component’s Parameters Template has one or more fields that contain spaces or special characters in the field name.This has been documented since Sitecore 6.2 in the “4.13 How to Implement a Rendering Settings Data Template” section of “Presentation Component Cookbook”.

Since I have hit this issue in Sitecore 9, it is still very much relevant today.

The solution

Well, we need to remove spaces from all these fields. However they are all stored on the Renderings and Final Renderings fields on all the content items that use them, so this is an impossible task to do manually.

Time to break out SPE

This script will find all parameters templates in the master DB and then recursively rename the fields and all usages in all versions and all languages.

SPE_FixRenderingParameters.ps1
function Write-LogExtended {
    param(
        [string]$Message,
        [System.ConsoleColor]$ForegroundColor = $host.UI.RawUI.ForegroundColor,
        [System.ConsoleColor]$BackgroundColor = $host.UI.RawUI.BackgroundColor
    )
    
    Write-Log -Object $message
    Write-Host -Object $message -ForegroundColor $ForegroundColor -BackgroundColor $backgroundColor
}
	
function Get-ItemBasedOnTemplate {
    param(
        [string]$TemplateId
    )

    $queue = New-Object System.Collections.Queue
    $processedLookup = New-Object System.Collections.Generic.HashSet[string]
    if(-not(Test-Path -Path "master:$($TemplateId)")) { return }
    $processedLookup.Add($TemplateId) > $null  
    Get-ItemReferrer -Id $TemplateId -ItemLink | 
        Where-Object { $_.SourceItemID } | 
        ForEach-Object { $queue.Enqueue($_.SourceItemID) }

    $database = Get-Database -Name "master"
    while($queue.Count -and ($referrerId = $queue.Dequeue())) {
        if($processedLookup.Contains($referrerId)) { continue }
        $processedLookup.Add($referrerId) > $null
        $referrer = $database.GetItem($referrerId)
        if(!$referrer) { continue }
        if($referrer.Paths.FullPath.StartsWith("/sitecore/templates")) { 
            if($referrer.Name -eq "__Standard values") { continue }
            foreach($referrerItemLink in Get-ItemReferrer -Id $referrerId -ItemLink | Where-Object { $_.SourceItemID }) {
                $queue.Enqueue($referrerItemLink.SourceItemID)
            }
            $itemTemplate = [Sitecore.Data.Managers.TemplateManager]::GetTemplate($referrerId, $database)
        } else {
            $itemTemplate = [Sitecore.Data.Managers.TemplateManager]::GetTemplate($referrer)
        }

        if ($itemTemplate -and $itemTemplate.DescendsFromOrEquals($TemplateId)) {
            $referrer
        }
    }
}

New-UsingBlock (New-Object Sitecore.Data.BulkUpdateContext) {

	$baseTemplateId = "{8CA06D6A-B353-44E8-BC31-B528C7306971}"
	$baseModuleId = gi -Path master: -ID "{0FDE5971-3F03-427C-AD45-BE76DAD03A88}"
	$templates = Get-ItemBasedOnTemplate -TemplateId $baseTemplateId
	$templates += $baseModuleId
	$templates | ForEach-Object {
	    $item = gi -Path master: -ID $_.ID
	    $item.FullPath
		Write-LogExtended "[Processing] $($item.Name) $($item.ID)" 
		$fieldsToRename = Get-ChildItem $item.FullPath -recurse | Where-Object { $_.TemplateName -eq 'Template field' -and $_.Name -match ' ' }
		if ($fieldsToRename.Count -gt 0) {
			
			Get-ItemReferrer -Item $_ | Where-Object { $_.TemplateName -match 'rendering' } |  ForEach-Object {
				#rendering
				Get-ItemReferrer -Item $_ |  ForEach-Object {
					#content item
					#Write-LogExtended "[Checking] $($_.FullPath)"
					
					Get-Item -Path master: -ID $_.ID -Version * -Language * |  ForEach-Object {
					
						$renderings = Get-ItemField -IncludeStandardFields -Item $_ -ReturnType Field -Name "__Renderings" | Where-Object { $_.ContainsStandardValue -eq $false }
						$finalrenderings = Get-ItemField -IncludeStandardFields -Item $_ -ReturnType Field -Name "__Final Renderings" | Where-Object { $_.ContainsStandardValue -eq $false }
						$renderingsvalue = ''
						$finalrenderingsvalue = ''
						if (($renderings -ne $null -and $renderings.Value.length -gt 0) -or ($finalrenderings -ne $null -and $finalrenderings.Value.length -gt 0)) {
							if ($renderings -ne $null)
							{
								$renderingsvalue = $renderings.Value
							}
							if ($finalrenderings -ne $null)
							{
								$finalrenderingsvalue = $finalrenderings.Value
							}
								
							foreach ($field in $fieldsToRename){
								
								$oldname = $field.Name
								$newname = $field.Name.Replace(" ", "")
								if ($renderings -ne $null -and $renderings.Value -match $oldname)
								{
									Write-LogExtended "[Set] [Renderings] field $oldname to $newname"
									$renderingsvalue = $renderingsvalue.Replace($oldname, $newname)
								}
								if ($finalrenderings -ne $null -and $finalrenderings.Value -match $oldname)
								{
									Write-LogExtended "[Set] [Final Renderings] field $oldname to $newname"
									$finalrenderingsvalue = $finalrenderingsvalue.Replace($oldname, $newname)
								}
							}
							if ($renderingsvalue -ne '' -or $finalrenderingsvalue -ne '') {
								Write-LogExtended "[Update] $($_.FullPath) $($_.ID) Language $($_.Language) Version $($_.Version)"
								$_.Editing.BeginEdit()
								if ($renderingsvalue -ne '') {
									$_.Fields["__Renderings"].Value = $renderingsvalue
								}
								if ($finalrenderingsvalue -ne '') {
									$_.Fields["__Final Renderings"].Value = $finalrenderingsvalue
								}
								$_.Editing.EndEdit() | out-null
							}
						}
					}
				}
			}
			
			foreach ($field in $fieldsToRename){
				Write-LogExtended "[Update] $($field.FullPath)"
				$oldname = $field.Name
				$newname = $field.Name.Replace(" ", "")
				$field.Editing.BeginEdit()
				$field["Title"] = $oldname
				$field.Name = $newname
				$field.Editing.EndEdit() | out-null
			}
		}
		
	}
}

Source: gist

You then need to make sure any code that has the field names hard coded is changed too.

Full stack trace for searches

Error Rendering Xslt: /xsl/system/webedit/hidden rendering.xslt: The ‘ ‘ character, hexadecimal value 0x20, cannot be included in a name. at System.Xml.XmlConvert.VerifyNCName(String name, ExceptionType exceptionType) at System.Xml.XmlQualifiedName.Verify() at System.Xml.Xsl.XsltArgumentList.AddParam(String name, String namespaceUri, Object parameter) at Sitecore.Mvc.Presentation.XsltRenderer.AddParameters(XsltArgumentList arguments) at Sitecore.Mvc.Presentation.XsltRenderer.GetArguments() at Sitecore.Mvc.Presentation.XsltRenderer.Render(TextWriter writer) at Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer.Render(Renderer renderer, TextWriter writer, RenderRenderingArgs args)