Monday 4 July 2016

Create Managed Meta data Column in Powershell Script

Add-PSSnapin Microsoft.SharePoint.PowerShell

Start-SPAssignment -Global

#The name for your new MMS column
$fieldName = "MMS Column"

#The site in which your new MMS column is available
$siteUrl = "http://sp2013vm:2015/sites/SharepointPub2015/"

#The name of the MMS Service Application where the Term Set lives
$mmsServiceName = "ManagedMetadata Service"

#The name of the Group where the Term Set lives
$mmsGroupName = "S-D"

#The name of the Term Set which your new MMS column will reference
$mmsTermSetName = "Check"

#Open the Site + Web
$site = Get-SPSite $siteUrl
$web = $site.RootWeb

#Gets a TaxonomySession object
$taxonomy = Get-SPTaxonomySession -Site $site

#Gets the GUID of the MMS Application
$sspId = $taxonomy.TermStores |
    Where-Object {$_.Name -eq $mmsServiceName} |
    Select-Object -ExpandProperty Id |
    Select-Object -ExpandProperty Guid

#Gets the GUID of the Term Set
$termSetId = $taxonomy.TermStores.Groups |
    Where-Object {$_.Name -eq $mmsGroupName} |
    Select-Object -ExpandProperty TermSets |
    Where-Object {$_.Name -eq $mmsTermSetName} |
    Select-Object -ExpandProperty Id |
    Select-Object -ExpandProperty Guid


#Creates a Field
$field = $web.Fields.CreateNewField("TaxonomyFieldType",$fieldName)
$web.fields.add($field)

#Updates with Term Set + SspId
$field = $web.fields.GetField($fieldName)
$field.TermSetId = $termSetId
$field.SspId =  $sspId
$field.Update()

Stop-SPAssignment -Global

No comments:

Post a Comment