Wednesday, June 17, 2015

How to Reset All Values for a Field using PowerShell

Standard
Today I ran into a situation where we had a Multilist with Search field for a bunch of items that had values set to items that no longer existed in our instance. This is often the case when you are in development and deleting and adding sections of the tree while testing things.

As you know, Sitecore tends to be unhappy when this happens. A common error message is the following:

Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).

So, I thought it would be a good exercise to write up a quick PowerShell script that would iterate over items in my tree, and reset the values for a field that I specify.

All you need to do is set the $fieldToReset and $path variable values to suit your needs.

 $fieldToReset = "My Field Name";  
 $path = "master:\sitecore\content\MyItem";  
 $items = Get-ChildItem -Path $path -Recurse   
 $items | ForEach-Object { if ($_.fieldToReset -ne $null) {$_.fieldToReset = ""} }  

Bless you PowerShell!


0 comments:

Post a Comment