SharePoint Online: PowerShell to Get Managed Metadata Field Value
Requirement: Get managed metadata field value in SharePoint Online using PowerShell.
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. In this post, we will cover how to retrieve the value of a managed metadata field using PowerShell in SharePoint Online.
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 the MMS column value, use this PnP PowerShell. For example, to retrieve the value for a field named “Classification” in a list named “Projects” in a SharePoint Online site, use the following script:
#Parameters
$SiteURL="https://crescent.sharepoint.com/Sites/pmo"
$ListName="Projects"
$FieldName="Classification" #Internal Name
$ListItemID="1"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#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 -Interactive
#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
}
}
In summary, retrieving the value of a managed metadata field in SharePoint Online is a simple process that can be accomplished using either CSOM or PnP PowerShell script cmdlets. By following the examples provided in this article, you should be able to retrieve the value of a managed metadata field and use it to categorize and organize your content in SharePoint Online.
To update a managed metadata field value, use: SharePoint Online: PowerShell to Update Managed Metadata Field Value
What if I want to retrieve multi values for the whole list and not just one item?
Here you go: SharePoint Online: Get All List Items using PowerShell Script