TransWikia.com

Powershell script for fetching checklist field value

Sitecore Asked on August 23, 2021

I would like to get checklist field value display name instead of raw values and would like to update its value based on display name.
For example I have checklist field values :

  1. Test 1
  2. Test 2

Based on its display name can we update its value in the Content Editor using powershell script in Sitecore.

One Answer

In order to read the field type Checklist in Sitecore Powershell, you need to cast the field as a MultilistField.

You can access field types allowing the user to select multiple items, including Checklist, Multilist, Treelist, and TreelistEx, by using the Sitecore.Data.Fields.MultilistField class.

You can use the Sitecore.Data.Fields.MultilistField.GetItems() method to access a list of Sitecore.Data.Item.Item objects representing the items specified by the field. For example, the following C# code shows you how to iterate over the items specified in the Multilist field named related.

More information at How to access Checklist, Multilist, Treelist, and TreelistEx fields.

SPE Script to read the checklist.

$item = Get-Item -Path master -ID "{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}"

$rawIds = [Sitecore.Data.Fields.MultilistField]$item.Fields["Your checklist field name"]

$selectedItems = $rawIds.GetItems()

foreach($selectedItem in $selectedItems){
    $selectedItem.DisplayName
}

I have created a Sitecore field on the sample template named as My Checklist with the field type as Checklist. The above script retrieve the Default Sitecore Home Item via the item id and then assign the raw value of the field My Checklist to the $rawIds.

As it is cast as MultilistField, you have the GetItems method available. This method returns you an array of Selected Items. From here, you can perform your different checks on the item display name etc...

Result

enter image description here

Correct answer by Hishaam Namooya on August 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP