5

I'm having a strange problem. I try to populate items' property which is of managed metadata (taxonomy) type and the update method doesn't work.

This field has "allow multiple values" property enabled and as long as I send one taxonomy type item to the field it gets stored. When I try to use more than one record and store with update on the item nothing changes.

So, it looks like this:

$item["TaxProp"] = "-1;#TaxItem1|26797705-ef0f-4d49-b7d8-f23112a325aa"
$item["TaxProp"]

After the last line I receive:

 WssId Label        TermGuid                                  ValidatedString
 ----- -----        --------                                  ---------------
 56    TaxItem1     26797705-ef0f-4d49-b7d8-f23112a325aa

Then I use $item.Update() and the data is stored properly in the list item.

But if I try to something like this:

$item2["TaxProp"] = "-1;#TaxItem1|26797705-ef0f-4d49-b7d8-f23112a325aa;#-1;#TaxItem2|c1b9fb3b-8655-4dd8-986c-cab898184d7d"
$item2["TaxProp"]

After the last line I receive:

 WssId Label        TermGuid                                  ValidatedString
 ----- -----        --------                                  ---------------
 56    TaxItem1     26797705-ef0f-4d49-b7d8-f23112a325aa
 56    TaxItem2     c1b9fb3b-8655-4dd8-986c-cab898184d7d

That is what I want to store, so I call $item2.Update()

After calling update no error occurs and nothing is stored. If I call $item2["TaxProp"] again now Powershell returns nothing.

What am I doing wrong?

5

You can set a MultiValue Managed Metadata field by creating a TaxonomyFieldValueCollection and add this collection to the field by using the SetFieldValue method.

First get the managed metadata field

A possible way ( there are more ways ) to do this is to get the field by id

$field = $item.Fields | ? {$_.Id -eq $fieldId}
$tf = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$field 

Check if its a MultipleValues field and get the termset and create a TaxonomyFieldValueCollection. In the example below all available terms will be added.

if ($tf.AllowMultipleValues) {
    $ts = Get-SPTaxonomySession -Site $item.Web.Site
    $tstore = $ts.DefaultSiteCollectionTermStore
    $termSet = $tstore.GetTermSet($tf.TermSetId)
    $taxCollection = new-object Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection($field)
    $termSet.GetAllTerms() | %{
        $taxonomyFieldValue = new-object Microsoft.SharePoint.Taxonomy.TaxonomyFieldValue($field)
        $taxonomyFieldValue.TermGuid = $_.Id
        $taxonomyFieldValue.Label = $_.Name
        $taxCollection.Add($taxonomyFieldValue)
    }
}

If you want to add a term by Id you can use for example

$term1 = $termSet.Terms | ? {$_.Id -eq "26797705-ef0f-4d49-b7d8-f23112a325aa"} 
$taxonomyFieldValue = new-object Microsoft.SharePoint.Taxonomy.TaxonomyFieldValue($field)
$taxonomyFieldValue.TermGuid = $term1.Id
$taxonomyFieldValue.Label = $term1.Name
$taxCollection.Add($taxonomyFieldValue)

Finally add the collection to the field and update your item

$tf.SetFieldValue($item, $taxCollection)
$item.SystemUpdate($false);
| improve this answer | |
  • Thank you very much for your answer. I did some modifications to this solution ro better suit my needs but overall answer is the correct solution to this problem. – user21367 Dec 15 '13 at 13:08

Your Answer

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

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