Trying to add a keyword to the keyword-multilist field with SPE.

$items = Get-ChildItem -Path "master:\sitecore\content\Website\site\subnode" -Recurse
$items | ForEach-Object {
    [Sitecore.Data.Fields.MultilistField]$field = $items.Fields["Keywords"]

    $items.Editing.BeginEdit()
    $field.Add("{6D1EACDD-0DE7-4F3D-B55A-2CAE8EBFF3D0}")
    $items.Editing.EndEdit()
}

The idea is to add the same keyword for each item below the subnode, and to every language version. An item can occure in more than one language, and the keywords-section can be empty or have one or more values. SPE returns "You cannot call a method on a null-valued expression." - so I'm kinda stuck...

With a lot of help from Sitecore friends, this is the solution:

#The guid of the keyword needed to be appended: {6D1EACDD-0DE7-4F3D-B55A-2CAE8EBFF3D0} 

$items = Get-ChildItem -Path "master:\sitecore\content\home" -Recurse -Language * 
foreach($item in $items) {
$item.Keywords = (@() + $item.Keywords.GetItems().ID    + "{6D1EACDD-0DE7-4F3D-B55A-2 CAE8EBFF3D0}" | Select-Object -Unique) -join "|" 
}

So thanks a lot for the help to Kamruz Jaman, Mike Ian Reynolds, Michael West and Adam Najmanowicz!

The book now contains updated examples.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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