Accessing Taxonomies from PowerShell

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 "";

}
About these ads
This entry was posted in PowerShell for SharePoint 2010 and tagged , , . Bookmark the permalink.

2 Responses to Accessing Taxonomies from PowerShell

  1. Huq says:

    Nice! Thanks for the post.

  2. Ben McInerney says:

    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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s