none
Powershell - Create managed metadata site column - term

    Question

  • Hi,

    Can somebody PLEASE tell me how I can create a managed metadata site column and connect it to a term using Powershell?
    I need to create a site column connected to Request Type. See an excerpt of my termstore.

    Managed Metadata Service
      Customer
        Customer Enterprise Taxonmy
          Document
             Request
               Request type
                  TypeA
                  TypeB
                  TypeC

    Thanks!

    Monday, June 17, 2013 7:10 AM

All replies

  • You can create your term store using PowerShell, first you have to create group then term sets, terms....

    You can use below sample script but you have to write code as per your requirement. I will suggest you to create a formatted CSV file and using PowerShell read the CSV file and create the taxonomy

    $SPtaxonomySession=Get-SPTaxonomySession -Site "http://servername:11/"
    $termStore=$SPtaxonomySession.TermStores["ManagedMetadataService"]
    $temGroup=$termStore.CreateGroup("MMS Group")
    $termSet=$temGroup.TermSets["Legal"]
    $termOne=$termSet.CreateTerm("Legal Document",1033)
    $termStore.CommitAll()

    check these link for your reference

    http://jyothiregode.wordpress.com/2011/02/19/create-termset-programmatically-using-powershell-in-sharepoint-2010/

    http://matthewyarlett.blogspot.in/2012/07/creating-new-terms-in-taxonomy-store.html


    Mark ANSWER if this reply resolves your query, If helpful then VOTE HELPFUL


    Everything about SQL Server | Experience inside SQL Server -Mohammad Nizamuddin

    Monday, June 17, 2013 8:32 AM
  • Thanks for your reply. The termstore is already created.  I now need to create a SITE COLUMN that is connected to the term store on the term level.

    As stated I need to connect my managed metadata site column to "Request type".

    Managed Metadata Service
      Customer
        Customer Enterprise Taxonmy
          Document
             Request
               Request type
                  TypeA
                  TypeB
                  TypeC

    Thnx!

    Monday, June 17, 2013 8:48 AM
  • Hi,

    I understand that you want to create a site column and bind it to a term set. Here are the detailed steps to achieve this:

    1. To start, you need a reference to the termstore:

    function Get-TaxonomySessionDefault()

    {

      $centralAdmin = Get-SPWebApplication -IncludeCentralAdministration | Where {$_.IsAdministrationWebApplication} | Get-SPSite

      $session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($centralAdmin)

      return $session

    }

    function Get-TermStoreDefault()

    {

      $session = Get-TaxonomySessionDefault

      $serviceApp = Get-SPServiceApplication | Where {$_.TypeName -like "*Metadata*"}

      $termStore = $session.TermStores[$serviceApp.Name]   

      return $termStore;

    }

    1. Access the termstore and get a reference to a TermSet to which to bind your new field

    function Get-TermSet([string]$groupName, [string]$termSetName)

    {

      $termStore = Get-TermStoreDefault

      return $termStore.Groups[$groupName].TermSets[$termSetName]

    }

    1. Function that creates a managed metadata site column in an SPWeb

    function Create-TaxonomyField(

      [Microsoft.SharePoint.SPWeb]$web,

      [string]$staticName,

      [string]$displayName,

      [string]$fieldGroup,

      [string]$termStoreGroupName,

      [string]$termSetName

    )

    {

      $termSet = Get-TermSet $termStoreGroupName $termSetName

      $taxonomyField = $web.Fields.CreateNewField("TaxonomyFieldType", $displayName)

      taxonomyField.SspId = $termSet.TermStore.Id

      taxonomyField.TermSetId = $termSet.Id

      taxonomyField.AllowMultipleValues = $false

      taxonomyField.Group = $fieldGroup

      taxonomyField.StaticName = $staticName

      taxonomyField.ShowInEditForm = $true

      taxonomyField.ShowInNewForm = $true

      taxonomyField.Hidden = $false

      taxonomyField.Required = $false

      $web.Fields.Add($taxonomyField);

      $web.Update();

      return $taxonomyField

    }

    1. Create a function to add the field to a list

    function Add-FieldToList(

      [Microsoft.SharePoint.SPWeb]$web,

      [string]$fieldName,

      [string]$listTitle,

    )

    {

      $list = $web.Lits[$listTitle]

      $list.Fields.Add($web.Fields[$fieldName])

      $list.Update()

    }

    For more detailed information, please refer to this site:

    powershell script to add a managed metadata column: http://sharepoint.stackexchange.com/questions/27936/powershell-script-to-add-a-managed-metadata-column+sharepoint+2010+connect+a+a+manged+matadata+column+powershell

    Thanks,

    Entan Ming


    Entan Ming
    TechNet Community Support

    Tuesday, June 18, 2013 6:34 AM
    Moderator
  • Thank you for your reply. Unfortunatly this code will indeed match it to a term set.
    (return $termStore.Groups[$groupName].TermSets[$termSetName])

    In my example to the level:
    Managed Metadata Service
      Customer
        Customer Enterprise Taxonmy

    I need to set it to a TERM (request type):
    Managed Metadata Service
      Customer
        Customer Enterprise Taxonmy
          Document
             Request
               Request type
                  TypeA

    That would result in:
    (return $termStore.Groups[$groupName].TermSets[$termSetName].Terms["Request"].Terms["Request type"]
    But that will fail on the following line:

    taxonomyField.TermSetId = $termSet.Id

    With the following error and I cannot understand why:

    Exception setting "TermSetId": "Specified argument was out of the range of vali
    d values
    ."
    At line:1 char:8
    + $field. <<<< TermSetId = $termpie.Id
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : PropertyAssignmentException

    Tuesday, June 18, 2013 6:48 AM
  • I tried web.Fields.CreateNewField and web.Fields.Add($taxonomyField) as described in http://blogs.msdn.com/b/sharepointdev/archive/2012/04/16/programmatically-create-a-managed-metadata-list-column-mohammed-faizan.aspx

    but it does not work. when i debug with VS, i found that the spfield created has m_strInterName null and m_Index -1.

    As a workaround, i use SPFieldCollection.AddFieldAsXml https://msdn.microsoft.com/en-us/library/office/ms457586.aspx now.


    Monday, July 27, 2015 9:43 AM
    Moderator