Create TermSets at Multiple Levels using Hash Table in Powershell

Mvsujani
Posted by in SharePoint category on for Beginner level | Points: 250 | Views : 9706 red flag

Term Store Management using Powershell Script :
Creating Groups, Terms and Termsets using Powershell Script. HashTable is used to create multiple Terms and TermSets at a time. Purpose of this article is the usage of HashTable and Powershell script at multiple levels of Managed MetadatService to create Termsets and Terms.

Introduction

Term Store Management. Create Groups, TermSets and Terms using powershell Script. Hash table to create multiple Terms and Termsets at a time in Managed Metadata Service. This is to create multiple Terms at multiple levels in Term Store Management of Sharepoint.

Note : Should have administrative rights to run the code.

Objective

This is an article  to create TermSets at multiple levels to a Group using PowerShell Script and Hash Table.

Using the code


Step 1 :

Create Groups on the Managed Metadata Service :

         

Example: GroupName

          

Step 2 :

    Create TermSet under ‘GroupName‘ using PowerShell script is as follows:

    A.   Create a .ps  file giving the site URL and save it. Ex: PS1.ps. 

 

param(

    [string] $termSetName = $(Read-Host -prompt "Specify the Term Set name :") )

 

    $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}

    if ($snapin -eq $null)

    {

      Add-PSSnapin Microsoft.Sharepoint.PowerShell

    }      

 

    $taxonomySession = Get-SPTaxonomySession -Site 'http://siteurl'

    $termStore = $taxonomySession.TermStores["Managed Metadata Service"]

    $group = $termStore.Groups["GroupName"]

   

    $termSet = $group.TermSets | Where-Object { $_.Name -eq $termSetName }

    if($termSet -eq $null) {

      $termSet = $group.CreateTermSet($termSetName)

    }   

  $termStore.CommitAll()

Write-Host "Created Successfully"  $termSetName  "TermSet"

 

B.    Open the Windows PowerShell window as Administrator and execute it.

        Give the name of the TermSet as TermSet1 or any name as per the choice.

        

        TermSet1, TermSet2 are created as below in the Term Store Management:

          

Step 3 :

Create Terms under TermSets using a HashTable :

a)      Create a .ps file specifying in which Group and TermSet you wanted to add Terms and save it as a .ps file. Ex: PS2.ps

 

[string] $termSetName = $(Read-Host -prompt "Specify the Term Set name :")

 

[string] $TextfilePath = $(Read-Host -prompt "Specify the Terms file path (.txt) :")

      

$hash=Get-Content -Path $TextfilePath |

ConvertFrom-StringData

$hash

 

for($i = -1; $i -lt (($hash.Count)-1); $i++)

{  

    write-host $i

    write-host $hash[$i].values

   

     $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}

    if ($snapin -eq $null)

    {

      Add-PSSnapin Microsoft.Sharepoint.PowerShell

    }

 

    $taxonomySession = Get-SPTaxonomySession -site 'http://siteurl'

 

    $termStore = $taxonomySession.TermStores["Managed Metadata Service"]

 

    write-host "Connection made with term store -"$termStore.Name

 

    #Connect to the Group and Term Set

    $termStoreGroup = $termStore.Groups["GroupName"]

    $termSet = $termStoreGroup.TermSets["$termSetName"]   

  

    #Create term, term description, and a synonym

    $term = $termSet.CreateTerm($hash[$i].values, 1033)

  

 write-host "Term Created -"$term.Name

 

    #Update the Term Store

    $termStore.CommitAll()

 

}

 

b)      Create a hash table with all the Term values and save it as .txt file. Ex : TermLevel1.txt

 

TermSet1=India

TermSet2=Australia

TermSet3=Brazil

TermSet4=China

 

c)      Open the Window PowerShell Administrator window and execute it.

    

      Terms under Termset1 using Hash Table are created as below :

           

      Step 4 :

This is the step to create Terms under Terms which is a useful one to create multiple levels of Terms using Hash Table.

a)      Create a .ps file specifying in which Group, TermSet and the SubTermSet name you wanted to add Terms and save it as a .ps file. Ex: PS3.ps

[string] $TextfilePath = $(Read-Host -prompt "Specify the Terms file path (.txt) :")

$hash=Get-Content -Path $TextfilePath |

ConvertFrom-StringData

$hash 

 for($i = -1; $i -lt (($hash.Count)-1); $i++)

{

$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}

    if ($snapin -eq $null)

    {

         Add-PSSnapin Microsoft.Sharepoint.PowerShell

    }

    $taxonomySession = Get-SPTaxonomySession -site 'http://siteurl'

    $termStore = $taxonomySession.TermStores["Managed Metadata Service"]

    write-host "Connection made with term store -"$termStore.Name

       #Connect to the Group and Term Set

    $termStoreGroup = $termStore.Groups["GroupName"]

    $termSet1 = $termStoreGroup.TermSets["TermSet1"]

    $termSet2 = $termSet1.Terms["India"]  

    #Create term, term description, and a synonym

   $term = $termSet2.CreateTerm($hash[$i].values,1033)

    #Update the Term Store

    $termStore.CommitAll()

}

    b)      Create a hash table with all the Term values and save it as .txt file. Ex : TermLevel2.txt

 

TermSet1=Gujarat

TermSet2=Kerala

TermSet3=Tamilnadu

TermSet4=Haryana

 

c)      Open the Window PowerShell Administrator window and execute it.

              

        The whole hierarchy of the Groups, Terms and TermSets created looks like this:

                 

Conclusion

This completes creating Terms at Second level. This can be extended to any level of creating Terms.

Reference

http:// www.c-sharpcorner .com/UploadFile/anavijai/5815/

http://pacsharepoint .com/2010/06/create-new-term-in-managed-metadata.htmlhttp://pacsharepoint.com/2010/06/create-new-term-in-managed-metadata.html

Page copy protected against web site content infringement by Copyscape

About the Author

Mvsujani
Full Name: Sujani A
Member Level: Starter
Member Status: Member
Member Since: 10/24/2011 5:38:56 AM
Country: India



Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)