0

I am trying to create a new item in Sitecore from Powershell and using the below command in ISE

New-Item -Path "master:\content\home" -Name "A/B Demo" -ItemType "Sample/SampleType"

So my expectation is an item with "A/B Demo" gets created in CMS under home. But getting an error

New-Item : Could not find a part of the path 'master:\content\home\A\B Demo'

How should I overcome this error. Thanks in Advance!!

2
  • 4
    You cannot have / in item names. Pass your name through ItemUtil.ProposeValidItemName before storing it. Set your original name as DisplayName instead.
    – Mark Cassidy
    Commented Jan 11, 2021 at 13:29
  • @MarkCassidy: Ok. How to do it from Powershell? Is there any command equivalent to ItemUtil.ProposeValidItemName?
    – Yeshwanth
    Commented Jan 11, 2021 at 13:49

2 Answers 2

2

Use the Display name

first step you have to propose valid item name

$name= Sitecore.Data.Items.ItemUtil.ProposeValidItemName("A/B Demo")

or you can use the below function to clean the name

function SanitizeName{
 $args[0].Replace(".", "").Replace(" ", "").Replace("/", "").Replace("-", "").Replace("&", "").Replace(":", "").Replace("""", "").Replace("#", "")
}

to use the name "A/B Demo"

use this command

$itemPath="master:/sitecore/content/home"+"/"+$name
$item=New-Item -Path $itemPath -ItemType "Sample/SampleType"
$item.Editing.BeginEdit()
$item["__Display Name"] = "A/B Demo" 
$item.Editing.EndEdit()
0

As others already said in the comments, you cannot have / character in your item name and you should use the ItemUtil.ProposeValidItemName method.

If you need to use it in Sitecore Powershell, you can use the following line to validate/format/escape item's name:

$itemName = [Sitecore.Data.Items.ItemUtil]::ProposeValidItemName('A/B Tests')

You can also include the / character as part of the display name instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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