About Me

Having 12 years experience in Microsoft technologies.Since more than 7 years working in SharePoint technologies. Expert in providing consultation for SharePoint projects. Hands on with development and administration.

Friday 4 July 2014

Powershell Script - Create Term Sets & groups in SharePoint 2013 Managed Meta Data service

The following script will create various term set groups, term sets & terms. GUID is optional. The script will create log file to current directory where the script runs... This script will create two term groups & one term set for each respective group.

Steps involved

1. Save following script as psCreateTermSets.ps1


#-----------------------------------------------------------------------------
# Name:             psCreateTermSets.ps1 
# Description:      This script will create a list of termset groups,termsets,
#                    terms along with guids.
#                    
# Usage:            Run the script by passing the paramter SiteUrl,
#                   ManagedMetaDataServiceName
# Created By:       Vamsi Mohan Mitta
#-----------------------------------------------------------------------------

Param([Parameter(Mandatory=$true)]
      [String]
      $SiteUrl,
      [Parameter(Mandatory=$true)]
      [String]
      $ManagedMetaDataServiceName
)

if ((gsnp Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue) -eq $null){
    asnp Microsoft.SharePoint.Powershell
}

function Get-ScriptDirectory
{
 $Invocation = (Get-Variable MyInvocation -Scope 1).Value
 Split-Path $Invocation.MyCommand.Path
}

# Create term groups with guid
Function CreateTermGroup($termStore, $termGroupName, $GUID)
 {
     if ($termStore.Groups[$termGroupName] -eq $null)
      {
        $termGroup = $termStore.CreateGroup($termGroupName, $GUID)
        $termStore.CommitAll()
        write-host “Successfully created term group -” $termGroupName -ForegroundColor Green
      }
      else
      { write-host “Term Group already -” $termGroupName " exists" -ForegroundColor Blue }
  }


 Function CreateTermSets($termGroup, $termSet, $termSetGuid)
 {   
    $getTermGroup = $termStore.Groups[$termGroup]
    if($getTermGroup.TermSets[$termSet] -eq $null)
    {
    $createTermSet = $getTermGroup.CreateTermSet($termSet,[System.Guid]($termSetGuid))
    $termStore.CommitAll()
    write-host “Successfully created term set-” $termSet -ForegroundColor Green
    }
    else
      { write-host “Term Set already -” $termSet " exists" -ForegroundColor Blue }
 }

 #Get Current Physical Path
$currentPhysicalPath = Get-ScriptDirectory

$logfile=$currentPhysicalPath + "\log.log"

Start-Transcript $logfile

 #declare all respective term groups, term sets & their guids
 $grpConstructionProject = "Construction Project"
 $guidConsProject = "14a7dfbc-d818-46f4-afbc-578fe1fe65df"

 $grpITProject = "IT Project"
 $guidITProject = "f3036492-1f65-46ba-ba69-4b0b2272ca35"

 $termSetConsClient = "Client"
 $guidConsClient = "19a01208-e1e3-49be-9f8d-fdebcbf9fb83"

 $termSetConsIndustry = "Industry & sector"
 $guidConsIndustry = "2FC48649-FF10-4A2E-BF72-6C600EA3E1AF"

 $termSetITClient = "Client"
 $guidITClient = "32088415-f136-42da-af43-9bcc360e5a34"

 $termSetITIndustry = "Industry"
 $guidITIndustry = "d6f66023-3e31-48ab-b413-64a6cdbca1b2"

 $termSetITSubIndustry = "Sub-Industry"
 $guidITSubIndustry = "87567d4f-69fc-4259-82c7-88042d880b58"

 $termSetITTechnology = "Technology"
 $guidITTechnology = "9f18bc77-3d82-4816-8f02-569cb041a6b6"


$taxonomySite = get-SPSite $SiteUrl
#Connect to Term Store in the Managed Metadata Service Application
 $taxonomySession = Get-SPTaxonomySession -site $taxonomySite
 $termStore = $taxonomySession.TermStores[$ManagedMetaDataServiceName]
 write-host “Connection made with term store -” $termStore.Name -ForegroundColor Green

 #create term groups
 CreateTermGroup -termStore  $termStore -termGroupName $grpConstructionProject -GUID $guidConsProject
 CreateTermGroup -termStore  $termStore -termGroupName $grpITProject -GUID $guidITProject

 #Create term sets
 CreateTermSets -termGroup $grpConstructionProject -termSet $termSetConsClient -termSetGuid $guidConsClient
 CreateTermSets -termGroup $grpITProject -termSet $termSetITClient -termSetGuid $guidITClient
 
   #Create terms for construction project - Client
    $getTermGroup = $termStore.Groups[$grpConstructionProject]
    $getTermSet = $getTermGroup.TermSets[$termSetConsClient]
    $getTermSet.CreateTerm("Apple Corp",1033)
    $getTermSet.CreateTerm("Commonwealth Bank",1033)
    $getTermSet.CreateTerm("Energy Australia",1033)
    $getTermSet.CreateTerm("Government of South Australia",1033)
    $getTermSet.CreateTerm("Sony Music",1033)
    $termStore.CommitAll()
    write-host “Successfully created terms for term set-” $termSetConsClient -ForegroundColor Green
   
    #Create terms for IT project - Client
    $getTermGroup2 = $termStore.Groups[$grpITProject]
    $getTermSet2 = $getTermGroup2.TermSets[$termSetITClient]
    $getTermSet2.CreateTerm("AGL",1033)
    $getTermSet2.CreateTerm("CUG",1033)
    $getTermSet2.CreateTerm("Commonwealth Bank",1033)      
    $termStore.CommitAll()
    write-host “Successfully created terms for term set-” $termSetITClient -ForegroundColor Green

        Stop-Transcript
    Echo Finish

--------------------------------------------------------------------------------------------------------------------------
Step2: Create batch file to run the script. Save file in .bat format.

cd /d %~dp0
powershell -file  ./psCreateTermSets.ps1 -SiteUrl "https://xxxx..com" -ManagedMetaDataServiceName "MMS_Proxy"
pause


Happy powershell coding... please feel free to comment...

- vamsi