the following code snippet assumes that a meta data store exists in “http://MySP2010/sites/CoolSites/” site collection. And also assumes that you have a group called Marketing and all Term Sets are defined under this group.
In order to access the Term Store, you should created a connection to the Taxonomy Term Store. To do so, First create a Session and connect it to the desired metadata Service. in our example, our session is connected to “Managed Metadata Service” term store.
$url = "http://MySP2010/sites/CoolSites/" $web = Get-SPWeb $url $taxonomySession = Get-SPTaxonomySession -Site $web.Site $termStore = $taxonomySession.TermStores["Managed Metadata Service"];
Next, we should get the group that comprises our tem set. In our example, we have looped through all groups within the term store and examined the group names.
foreach($item in $termStore.Groups){ if($item.Name -eq "Marketing") { $group = $item } }
Once we gained access to the group, we can easily access to the Term Set, which is “Marketing Initiatives” in our example.
$termset = $group.TermSets["Marketing Initiative"];
To wrap this post, we have gone two levels deep in the term store and printed the combination of the “term | id”:
foreach($t in $termset.Terms){ Write-Host "----------First Level: ----------------"; Write-Host $t.Name"|"$t.Id ; Write-Host "-----Second Level-----------------"; foreach($t2 in $t.Terms){ Write-Host $t2.Name "|" $t2.Id } Write-Host ""; }
Here is everything together:
$url = "http://MySP2010/sites/CoolSites/" $web = Get-SPWeb $url $taxonomySession = Get-SPTaxonomySession -Site $web.Site $termStore = $taxonomySession.TermStores["Managed Metadata Service"]; foreach($item in $termStore.Groups){ if($item.Name -eq "Marketing") { $group = $item } } Write-Host "---->: " $group.Name $termset = $group.TermSets["Marketing Initiative"]; #$guid = New-Object System.Guid("c1353ee2-b2b2-4fc9-90a2-3b912390b6ac"); #$term = $termset.GetTerm($guid); foreach($t in $termset.Terms){ Write-Host "----------First Level: ----------------"; Write-Host $t.Name"|"$t.Id ; Write-Host "-----Second Level-----------------"; foreach($t2 in $t.Terms){ Write-Host $t2.Name "|" $t2.Id } Write-Host ""; }
Nice! Thanks for the post.
Hi there,
When you are getting the group, instead of iterating the Groups, why dont you just reference it directly (like you have for the TermSets)
$group = $termStore.Groups["Marketing"]
Cheers,
Ben