0

I have a document Library.

It contains many columns out of which I want to update the value for managed metadata column.

Suppose, the Value which I want to update is "My Value" and this value "My Value", exists in term store and also in Taxonomy Hidden List.

Below is my code to update the value:

#CAML Query to get Item From Library and Update
        $spQuery = New-Object Microsoft.SharePoint.SPQuery
        $spQuery.ViewFields  ="<FieldRef Name='My_x0020_Prod' />";
        $spQuery.ViewAttributes = "Scope='Recursive'";
        $spQuery.Query = "<Where>                                    
                                  <Eq>
                                    <FieldRef Name='Prod_x0020_ID' />
                                    <Value Type='Text'>" + $prodID  + "</Value>
                                 </Eq>                                         
                           </Where>"
        $col=$list.GetItems($spQuery) 
        foreach ($item in $col)  
        {  
           if($item.Count -gt 0)
            {

                $item["My_x0020_Prod"] = "My Value"
                $item.Update()

            } 
        } 

This code doesnt update the MMD item in the library and throws an error:

The given guid does not exist in the term store

Now, when I go back to the library and manually update the field for the MMD column, it works fine.

Programatically, this is not working and I think I am missing something.

Should I first get GUID from term store and then update the value in MMD?

0

Yes you cannot update MMD field whose value is not present in termstore

You need to first get the term value which you will be updating

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Taxonomy")
$site = Get-SPSite "http://site"
$session = New-Object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
$termStore = $session.TermStores["TermStore Name"]
$group = $termStore.Groups["Terms"]
$termSet = $group.TermSets["termset"]
$terms = $termSet.GetTerms(number of terms to be retrieved)
$term = $terms | ?{$_.Name -eq "your value"}

And then use $term to update in your code

 $termfield= [Microsoft.SharePoint.Taxonomy.TaxonomyField]$List.Fields["My_x0020_Prod"]

foreach ($item in $col)  
    {  
       if($item.Count -gt 0)
        {

            $termfield.SetFieldValue($item, $term)
            $item.SystemUpdate()

        } 
    }
  • I do not want to update any values in termstore. I just want to update a value for MMD type of column(in a library). Even then, should I goto termstore and get the term value? – user2598808 Mar 3 '17 at 12:25
  • Yes, the above code does not update value in termstore but gets the value from term store amd then updates a mmd field – rjv91 Mar 3 '17 at 15:00
  • Thank you for the response. I am working on the suggested code. $term is coming as empty. $_.Name is not a property which could be found. Any suggestions would be really helpful. Thanks Again ! – user2598808 Mar 6 '17 at 5:07
  • Are you getting any results in the $terms object.Also check if you have given the correct termstore name,group name termset name etc.. – rjv91 Mar 6 '17 at 5:15
  • Termstore Name, Group Name , Termset Name are correct. $termSet.GetTerms(1) does not return anything. I have mentioned (1) as I need only one term. Number of terms to be retrieved is 1 in my case. If I write $termSet.GetAllTerms() then all terms are returned. I need to search based on specific term. – user2598808 Mar 6 '17 at 5:28
0
#Create a Taxonomy Session
 $TaxonomySession = Get-SPTaxonomySession -Site $web.Site
 $TermStore = $TaxonomySession.TermStores[$termStoreName]
 $TermGroup = $TermStore.Groups[$termGroupName]
 $TermSet = $TermGroup.TermSets[$termSetName]

 #Below line of code will get the name, GUID and other properties from TermStore
 $Term = $Termset.Terms[$MMDValueToUpdate]

#This is the Library Item OOTB ID of the item which you want to update
 $item = $list.GetItembyID($ItemID)
 if($item.Count -gt 0)
 {
 $MMSField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$item.Fields[$FieldName]
 #If value exists in TermStore
 if($Term)
 {
 $MMSField.setFieldValue($item,$Term)
 $Item.SystemUpdate()
 Write-Host "Updated value as: " $Term.Name -backgroundcolo Green
 }
 else
{
Write-Host "Could not find termstore properties for : " $MMDValueToUpdate  -backgroundcolo Red
}

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.