2

I have used following PS script to update a manage metadata column. Not sure how to go by updating a managed-metadata column. I know I need to only populate with "10;#Wholesale Agreement" in SalesType field. Why the code is not updating? I am getting error but wont give me details. [Ed: Per later answer by original poster the following updated code is working].

$webUrl = "http://intranet.nissan.usa/sites/sentra"
$web = Get-SPWeb $webUrl
$list = $web.Lists["sales"]
$ct = "Sales Agreements"
$f = "SalesType" #Column Name      
$newTerm = "10;#Wholesale Agreement|1274c09f-4a41-453e-8422-60601d61bd11"

foreach ($item in $list.items)
{
    try {
        If ($item.ContentType.Name -eq $ct)
        {           
            $field = $list.Fields[$f]
            $fieldValue = $item[$f]

            if($fieldValue -eq $null)
            {               
                $item[$field] = $newTerm                
                $item.Update();                     
            }
        } 
        }   
    catch{
        Write-Host $item.Name " error occured" 
        }   
} 
#finally dispose the web
$web.dispose()
1
  • Any idea why the field (managed metadata type field) is not updating with the value..... Commented Oct 3, 2011 at 19:17

1 Answer 1

3

Here is the final version. I had to use SP Manager to get the guid of the term set. I am sure I can add more logic to PS script and look for actual term. But for now this works...

I updated the code with the working code in the original post and below.

$webUrl = "http://intranet.nissan.usa/sites/sentra"
$web = Get-SPWeb $webUrl
$list = $web.Lists["sales"]
$ct = "Sales Agreements"
$f = "SalesType" #Column Name      
$newTerm = "10;#Wholesale Agreement|1274c09f-4a41-453e-8422-60601d61bd11"

foreach ($item in $list.items)
{
    try {
        If ($item.ContentType.Name -eq $ct)
        {           
            $field = $list.Fields[$f]
            $fieldValue = $item[$f]
        if($fieldValue -eq $null)
        {               
            $item[$field] = $newTerm                
            $item.Update();                     
        }
    } 
    }   
catch{
    Write-Host $item.Name " error occured" 
    }   
}

finally dispose the web

$web.dispose()
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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