This post contains the code to update Managed Metadata column value in Library.
Add-PSSnapin Microsoft.Sharepoint.Powershell
cls
#Log Variables
$basePath = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
Set-Location $basePath
#SP variables
$web=$null ;
$list=$null;
$SiteURL = "https://mysharepointsite/en-us"# Library Site Url
$ListName = "Pages";# List Name
$FieldName="Document"
$termStoreName = "Managed Metadata Service Application"
$termGroupName = "Document_Content"
$termSetName = "DocumentName"
$MMDValueToUpdate = "Technical Doc"
#Pre-requisite checks
Try
{
#Web Check
$web = Get-SPWeb $SiteURL
if($web -eq $null)
{
Write-Host "Cannot find an SPSite object that contains the following Id or Url: $SiteURL" $_.Exception.Message
Exit
}
#List Check
$list = $web.Lists[$ListName]
if($list -eq $null)
{
Write-Host "Cannot find a List with the Name: $ListName in the web " $_.Exception.Message
Exit
}
}
Catch
{
Write-Host "Unable to connect to the Site, Please check the Url . Failed with error : " + $_.Exception.Message
Exit
}
if($list -ne $null)
{
try
{
#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
}
}
catch
{
Write-Host "Error : $_.Exception.Message" -foregroundcolor white -backgroundcolor Red
return
}
}
else
{
Write-Host "No List with Name "+$ListName
}
Comments
Post a Comment