Friday, November 20, 2015

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:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 
 
#Variables
$WebURL="https://portal.crescent.com/sites/Sales/"
$ListName="Team Docs"
$FieldName="Regions"
 
#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
}

 



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...