How to Add Managed Metadata Column to SharePoint List using PowerShell?
Requirement: Add a Managed Metadata Column to SharePoint List using PowerShell.
PowerShell Script to Add Managed Metadata Column in SharePoint List
Use this PowerShell to create a new Managed Metadata field in a SharePoint List:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Variables
$WebURL="https://portal.crescent.com/sites/Sales/"
$ListName="Team Docs"
$FieldName="Region"
#Get the web and List
$Web = Get-SPWeb $WebURL
$List = $Web.Lists[$ListName]
#Check if the column with same name exists in the list
if(!($List.Fields.ContainsField($FieldName)))
{
#Get the Termset from Term store
$TaxonomySession = Get-SPTaxonomySession -Site $web.Site
$TermStore = $TaxonomySession.TermStores["Managed Metadata Service"]
$TermGroup = $TermStore.Groups["Knowledge Portal"]
$TermSet = $TermGroup.TermSets["Regions"]
#Form the Taxonomy Field
$TaxonomyField = $List.Fields.CreateNewField("TaxonomyFieldType", $TermSet.Name)
$TaxonomyField.SspId = $TermSet.TermStore.Id
$TaxonomyField.TermSetId = $TermSet.Id
#Add the field to List
$List.Fields.Add($TaxonomyField)
$List.Update()
Write-host "Managed Metadata column Added successfully!"
}
else
{
Write-host "Managed Metadata column with the specific name already exists!" -f RED
}
Please note, You need to have MMS Service application up and running. Here
- Managed Metadata Service – is the MMS application proxy name
- Knowledge Portal – is the name of the term set group
- Regions – is the name of the term set
This Adds “Region” Metadata column to the given document library “Team Docs”
great script, small change:
$TaxonomyField = $List.Fields.CreateNewField(“TaxonomyFieldType”, $TermSet.Name)
should be:
$TaxonomyField = $List.Fields.CreateNewField(“TaxonomyFieldType”, $FieldName)
otherwise the name of the field is always equal to the name of the termset and the parameter stays unused.
I am good all the way do the “Regions” termset, I have one more child term set inside that, how do I call it?
The logic is TermStores.Groups.TermSets.Terms.Terms.Terms…
So after the TermSets there are “nested” Terms.
$TermStore = $TaxonomySession.TermStores[“Managed Metadata Service”]
$TermGroup = $TermStore.Groups[“Knowledge Portal”]
$TermSet = $TermGroup.TermSets[“Regions”]
$Terms = $TermSet.Terms[“Terms”]
$Terms1 = $Terms.Terms[“Terms1”]
$Terms2 = $Terms1.Terms[“Terms2”] and so on…
Hello Salaudeen,
Thank You for the excellent script. I have a question. I wanted this script to add a managed metadata column to all the document libraries in a site collection. Do you know if that is possible and what can be the script changes to accomplish it?
Thanks,
Ricky
thanks sir 😉
Hello please, how can i define this column to be multivalue ? using powershell !
Just set: $TaxonomyField.AllowMultipleValues = $True