Showing posts with label updatelistitems. Show all posts
Showing posts with label updatelistitems. Show all posts

Sunday, November 8, 2020

PowerShell: How to update column with managed Metadata ?

Below PS script to update a manage metadata column. We can update columnby updating a managed-metadata column. Format of value is  "16;# Warriors" in groupType field.

$weburl="/sites/manager"
$web = Get-SPWeb $webUrl
$list = $web.Lists["department"]
$ct = "Deptgroup"
$f = "groupType" #Column Name      
$newTerm = "10;#test
 Agreement|1273c09f-4ar1-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()
#updatelisitems #managedmetadata #columnname