Wednesday 20 March 2013

Clear (Delete) the value of a taxonomy field using PowerShell

[Update, 3/July/2013]: I've converted this PowerShell into a script which can be downloaded from the Microsoft TechNet Gallery, here: http://gallery.technet.microsoft.com/sharepoint/Update-or-Clear-Metadata-94959cb3

The script makes it easy to clear or update the metadata values of a metadata column.

E.g.

Import-Module C:\Scripts\Update-MetadataField.ps1
$listitems = (Get-SPWeb http://devmy101).Lists["mylistwithdatainit"].Items 
Clear-MetadataField -items $listitems -MetadataFieldInternalName metadataField -ClearAllItems

[Original Article]

Yesterday I got stumped working out how to clear the value of taxonomy field when helping someone on the Microsoft forums. I could clear the value of the field using the TaxonomyField.ParseAndSetValue(item, value) method of the field, setting the value to $null, but it only worked if the list item belonged to a list. If the item was in a document library, the value wouldn't be cleared. Eventually (after a little persistence), I worked it out, and thought I'd share it here.
The proper way to clear a taxonomy field (which seems obvious upon reflection!), is to get an empty taxonomy value, using the Taxonomy.GetFieldValue method, and then pass it to the TaxonomyField.SetFieldValue method.

The only other difference between working with lists and document libraries, is the way in which we retrieve the list item.

Working with Lists
$web = get-spweb http://my
$list = $web.Lists["my list"]
$item = $list.items[1] #Get an item that currently has a value set in the Taxonomy (Managed Metadata) field
$taxField = $item.Fields["metadataField"] -as [Microsoft.SharePoint.Taxonomy.TaxonomyField] 
$taxFieldValue = $taxField.GetFieldValue("");
$taxField.SetFieldValue($item,$taxFieldValue)
$item.Update()

Working with Document Libraries
$web = get-spweb http://my
$list = $web.Lists["document library"] -as [Microsoft.SharePoint.SPDocumentLibrary]
$item = $list.items[1] #Get an item that currently has a value set in the Taxonomy (Managed Metadata) field
$url = [String]::Format("{0}{1}",$web.Url, $item.File.Url)
$file = $web.GetFile($url)
if($file.CheckOutStatus -eq "None")
{
$file.CheckOut()
$fileItem = $file.Item
$taxField = $fileItem.Fields["metadataField"] -as [Microsoft.SharePoint.Taxonomy.TaxonomyField]
$taxFieldValue = $taxField.GetFieldValue("");
$taxField.SetFieldValue($fileItem,$taxFieldValue)
$fileItem.Update()
$file.CheckIn("Updated Taxonomy Field Value")
}
else
{
$msg = [String]::Format("This file, {0}, is checked out and cannot be edited at the moment",$file.Name)
Write-Host $msg -ForegroundColor DarkYellow
}

Other tips...

Getting an item by it's ID
$item = $list.GetItemById(12)

Looping through the list
$web = get-spweb http://my
$list = $web.Lists["my list"]
$items = $list.items
$taxField = $list.Fields["metadataField"] -as [Microsoft.SharePoint.Taxonomy.TaxonomyField] 
$taxFieldValue = $taxField.GetFieldValue("");
foreach($item in $items)
{
$taxField.SetFieldValue($item,$taxFieldValue)
$item.Update()
}

3 comments:

  1. Hi Matthew, I'm having problems updating MMD fields using PS at the moment, and your post has been invaluable in getting me half the way there. One question though: If you didn't want to update the field to blank but instead wanted to give it a value from the TermStore, which already exists e.g. "apple", can you use $taxField.GetFieldValue("apple")?

    ReplyDelete
    Replies
    1. Hi Robin,

      Glad to hear the post has been helpful!

      When you get the value of a term, you need to know its GUID. An easy way to get this is by listing out the termset's terms and corresponding ID's. I blogged about this here: http://matthewyarlett.blogspot.co.uk/2012/08/bulk-updating-taxonomy-field-value-on.html

      Essentially, your code would look like this (except it needs to use your values!)

      $ts = Get-SPTaxonomySession -Site http://devmy101
      $tstore = $ts.TermStores[0]
      $tgroup = $tstore.Groups["Countries"]
      $tset = $tgroup.TermSets["United Kingdom"]
      #List out all the terms and their ID's
      $tset.Terms | FT Name,Id
      #Find the term you want to use, and use it's id to get the term
      $term = $tset.Terms[[System.Guid]("8b9ff507-e5a9-4bb9-8dd4-c2ccc7a45a9d")]

      $web = get-spweb http://devmy101
      $list = $web.Lists["mylistwithdatainit"]
      $items = $list.items
      $taxField = $list.Fields["metadataField"] -as [Microsoft.SharePoint.Taxonomy.TaxonomyField]
      foreach($item in $items)
      {
      $taxField.SetFieldValue($item,$term)
      $item.Update()
      }

      Delete
    2. Wow, thanks very much for the fast response, much appreciated!

      Delete