3

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.

1 Answer 1

4

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

5
  • above code is showing only selected values of the checklist field. I would like to get all the values including unselected.
    – Ashish
    Jul 18, 2020 at 15:58
  • Then, the only way is that you need to get the items from the datasource then use the above to compare if needed Jul 18, 2020 at 16:00
  • Okay, i will check how to get items value from datasource.
    – Ashish
    Jul 18, 2020 at 16:06
  • I got the datasource result and based on your code i am comparing the items. Thanks
    – Ashish
    Jul 18, 2020 at 17:08
  • 1
    $item.PSFields will give you typed objects. Jul 19, 2020 at 20:27

Your Answer

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

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