0

Currently I have this snippet to set update a taxonomy field:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Taxonomy")
$spWeb = Get-SPWeb "http://intranet/sitecollection/somesite"

$session = New-Object Microsoft.SharePoint.Taxonomy.TaxonomySession($spWeb.Site)
$termStore = $session.TermStores["Managed Metadata Service"]
$group = $termStore.Groups["Terms"]
$termSet = $group.TermSets["Projects"]

$term = $termSet.GetTerms("My Project",$true)

$list = $spWeb.Lists["Documents"]

foreach($item in $list.Items){
    $spItem = [Microsoft.SharePoint.SPListItem]$item
    $taxField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$spItem.Fields["Project"]
    $taxField.SetFieldValue($spItem,$taxField)
    $spItem.Update()
}

This will work at the root site collection level, but does not work at the site level.

If I manually set the term and then grab the ID;#My Project value, I can then set it. I'm hoping for a more general way to do this as there may be multiple locations I need to do this.

Essentially if I change it to this:

$taxFieldValue = $taxField.GetFieldValue("5;#My Project");

$taxField.SetFieldValue($spItem,$taxFieldValue)

it will work. But if I try to grab the term, it won't set it. Site collection root level it works, on subsites it does not.

Edit, just realized I am using getTerms which even though it only finds one, it returns a collection.

In case anyone needs for reference:

$term = $termSet.GetTerms("My Project",$true)[0]

or something like

$terms = $termSet.GetAllTerms()
$term = $terms["My Project"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.