Field Types
Examples for managing complex field types such as MultilistField and NameValueListField.

Edit MultilistField

Example: The following demonstrates how to set a field to a known list of Ids. The Id is already converted to a GUID string.
1
# Hardcoded list of Ids.
2
$item.Editing.BeginEdit()
3
$item["Allowed Controls"] = "{guid1}|{guid2}|{guid3}"
4
$item.Editing.EndEdit()
Copied!
1
# Array of Ids.
2
$array = [System.Collections.ArrayList]@()
3
$array.Add({guid1}) > $null
4
$array.Add({guid2}) > $null
5
$ids = [System.String]::Join("|", $array)
6
$item.Editing.BeginEdit()
7
$item["Allowed Controls"] = $ids
8
$item.Editing.EndEdit()
Copied!
Example: The following replaces an instance of an Id with an alternate Id. The Id is already converted to a GUID string.
1
[Sitecore.Data.Fields.MultilistField]$field = $item.Fields["Allowed Controls"]
2
$item.Editing.BeginEdit()
3
$field.Replace("{493B3A83-0FA7-4484-8FC9-4680991CF742}","{493B3A83-0FA7-4484-8FC9-4680991CF743}")
4
$item.Editing.EndEdit()
Copied!
Example: The following adds new Ids to an existing list. Makes use of the Sitecore.Text.ListString class.
1
[Sitecore.Text.ListString]$ids = $item.Fields["Rendering"].Value
2
$ids.AddAt(0,"{guid1}") > $null
3
$ids.Add("{guid2}") > $null
4
$ids.Add("{guid3}") > $null
5
6
$item.Editing.BeginEdit()
7
$item.Fields["Rendering"].Value = $ids.ToString()
8
$item.Editing.EndEdit() > $null
Copied!
Example: The following appends a an ID to a set of items in all languages. It verifies that the field Keywords exists.
1
$items = Get-ChildItem -Path "master:\sitecore\content\home" -Recurse -Language *
2
foreach($item in $items) {
3
if ($item.Keywords -and $item.Keywords.Length -gt 0) {
4
$item.Keywords = $item.Keywords + "|{guid}"
5
} else {
6
$item.Keywords = "{guid}"
7
}
8
}
Copied!
Example: The following example gets all of the items of a MultilistField and append a specific ID, ensuring that it's delimited with the | character.
1
$items = Get-ChildItem -Path "master:\sitecore\content\home" -Recurse -Language *
2
foreach($item in $items) {
3
$item.Keywords = (@() + $item.Keywords.GetItems().ID + "{6D1EACDD-0DE7-4F3D-B55A-2CAE8EBFF3D0}" | Select-Object -Unique) -join "|"
4
}
Copied!
Example: The following example extracts the items from a 'keywords' field, comma separates the values, and then outputs to a report.
1
function Get-KeywordsAsString($item) {
2
[Sitecore.Data.Fields.MultilistField] $field = $item.Fields["Keywords"]
3
$items = $field.GetItems()
4
$strArray = $items | Foreach { $_.DisplayName }
5
$strArray -join ', '
6
}
7
Get-ChildItem -Path 'master:\content\home\path-to-item' `
8
| Show-ListView -Property Name, Language, Version, ID, TemplateName, ItemPath, `
9
@{ Label = "KeywordsAsString"; Expression = { Get-KeywordsAsString($_) } }
Copied!

Edit NameValueListField

Example: The following example gets all of the name/value pairs of a NameValueListField and appends a new pair.
1
$item = Get-Item -Path "master:" -ID "{371EEE15-B6F3-423A-BB25-0B5CED860EEA}"
2
3
$nameValues = [System.Web.HttpUtility]::ParseQueryString($item.UrlMapping)
4
5
# Here you can add or remove name/value pairs
6
$nameValues["^/ab[cde]/quot;] = "/somewhere/fun?lang=en"
7
8
foreach($key in $nameValues.AllKeys) {
9
$nameValues[$key] = [Uri]::EscapeDataString($nameValues[$key])
10
}
11
12
$item.UrlMapping = [Sitecore.StringUtil]::NameValuesToString($nameValues,"&")
Copied!
Last modified 3mo ago
Export as PDF
Copy link