2

I have a Name Value List field in Sitecore called "PageSchema". I need to add another name, and value to this PageSchema field using PowerShell.

How can I do it from PowerShell? What I want is something like this.

{
        $ArticleItem.Editing.BeginEdit()
        try {
             NameValueCollection data = new NameValueCollection()
             data.Add("name", "value")
             ArticleItem["PageSchema"] = StringUtil.NameValuesToString(data, "&")
        }
}

4 Answers 4

3

Here is an example taken from the official docs.

Example: The following example gets all of the name/value pairs of a NameValueListField and appends a new pair.

$item = Get-Item -Path "master:" -ID "{371EEE15-B6F3-423A-BB25-0B5CED860EEA}"

$nameValues = [System.Web.HttpUtility]::ParseQueryString($item.UrlMapping)

# Here you can add or remove name/value pairs
$nameValues["^/ab[cde]/$"] = "/somewhere/fun?lang=en"

foreach($key in $nameValues.AllKeys) {
    $nameValues[$key] = [Uri]::EscapeDataString($nameValues[$key])
}

$item.UrlMapping = [Sitecore.StringUtil]::NameValuesToString($nameValues,"&")
1

You can use the below script to update your PageSchema field value for the Name Value List field.

$item = Get-Item -Path "master:\content\home"

$nameValues = [System.Web.HttpUtility]::ParseQueryString($item.PageSchema)

$nameValues["Key3"] = "Value3"

Write-Host "Result before update $($item.PageSchema)"

$item.PageSchema = [Sitecore.StringUtil]::NameValuesToString($nameValues,"&")

Write-Host "Result after update $($item.PageSchema)"

In this case, I tried on the home item, Refer to the following screenshot for more clarity.

1. Have 2 key value items initially on the Home item.

enter image description here

2. Result of executing the above script.

enter image description here

3. Updated values of Name Value List field.

enter image description here

Hope it helps!

1

You can use two ways to do it.

Option 1:

As per the Sitecore Official Document, If you want to add a new key-Value in the Name Value List field. Here is the code that you can use.

# Get your Item
$item = Get-Item -Path "master:" -ID "{1CE0717B-0225-48A1-A857-50A59EE1BF85}"

$nameValues = [System.Web.HttpUtility]::ParseQueryString($item.Schema)

# Here you can add name/value pair
$nameValues["Key4"] = "Value4"

$item.Schema = [Sitecore.StringUtil]::NameValuesToString($nameValues,"&")

Option2:

If you will see the raw value of the field, the format looks like this.

Key1=Value1&Key2=Value2&Key3=Value3

So you can add a new key on the item edit by using below code.

$item = Get-Item -Path "master:" -ID "{1CE0717B-0225-48A1-A857-50A59EE1BF85}"
#$item.Editing.BeginEdit()
#$item.Fields["Schema"].Value = $item.Fields["Schema"].Value + "&" + "Key3=Value3"
#$item.Editing.EndEdit()

In both of the cases, the value will be added to your field like below.

enter image description here

Hope this will help you.

0

You can try one if you want for bulk item.

enter image description here

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.