SharePoint Online: PowerShell to Get Managed Metadata Field Value

Requirement: Get managed metadata field value in SharePoint Online using PowerShell
sharepoint online powershell get managed metadata field value

SharePoint Online: PowerShell to Get Managed Metadata Field Value
Managed metadata is used to provide a consistent structure of terms throughout an organization. Managed metadata fields store the term GUID internally as its value. To get the value of a Managed metadata column, use this PowerShell.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
#Set parameters
$SiteURL="https://crescent.sharepoint.com/Sites/pmo"
$ListName="Projects" 
$FieldName="Classification" #Internal Name
$ListItemID="1"

#Get Credentials to connect
$Cred = Get-Credential
  
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
#Get the List Item
$List = $Ctx.Web.lists.GetByTitle($ListName)
$ListItem = $List.GetItemById($ListItemID)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()
 
#Get Managed Metadata Field Value
$MMSFieldValue = $ListItem[$FieldName]
Write-host $MMSFieldValue.Label
Write-host $MMSFieldValue.TermGuid

PnP PowerShell to Get Managed Metadata Column Value
Similarly, to retrieve the value of MMS column value, use this PnP PowerShell.
#Parameters
$SiteURL="https://crescent.sharepoint.com/Sites/pmo"
$ListName="Projects" 
$FieldName="Classification" #Internal Name
$ListItemID="1"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin

#Get List items from the list
$ListItem = Get-PnPListItem -List $ListName -Id $ListItemID -Fields $FieldName

#Get Managed Metadata Field Value
$MMSFieldValue = $ListItem[$FieldName]
Write-host $MMSFieldValue.Label
Write-host $MMSFieldValue.TermGuid

Retrieve Managed Metadata Field value that allows multiple values using PowerShell:
To get multi-valued MMS Field value, use:
#Parameters
$SiteURL="https://crescent.sharepoint.com/Sites/pmo"
$ListName="Projects" 
$FieldName="Classification" #Internal Name
$ListItemID="1"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin

#Get an item from the list
$ListItem = Get-PnPListItem -List $ListName -Id $ListItemID -Fields $FieldName

#Get the Field
$Field = Get-PnPField -List $ListName -Identity $FieldName 

#Check if the field allows Multiple values
If($Field.TypeAsString -eq "TaxonomyFieldType")
{
    #Get Managed Metadata Field Value
    $MMSFieldValue = $ListItem[$FieldName]
    Write-host $MMSFieldValue.Label
    Write-host $MMSFieldValue.TermGuid
}
Else #TaxonomyFieldTypeMulti
{
    $MMSFieldValue = $ListItem[$FieldName] | ForEach-Object {
        Write-host $_.Label
        Write-host $_.TermGuid
    }
}
To update a managed metadata field value, use: SharePoint Online: PowerShell to Get Managed Metadata Field Value

2 comments:

Please Login and comment to get your questions answered!

Powered by Blogger.