Have a problem when trying to iterate through termstore in Managed Metadata service application.

Termstore architecture is shown below:

              Level 1 Term
                    Level 2 Term
                         Level 3 Term
            Level 1 Term
                     Level 2 Term
                         Level 3 Term


I'm using the following powershell script:

$session = New-Object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
$termStore = $session.TermStores[0]
$group = $termStore.Groups["%Termgroup name%"]
$termSet = $group.TermSets["%Termset name%"]

When i use the following command:

$terms = $termSet.Terms
$term = $terms | ?{$_.Name -eq "%Level 3 Term%"}

$term is null because $terms array contains only Level 1 Terms (including all child terms).

If i try following:

$terms = $termSet.GetAllTerms

$terms variable contains all terms (should according to method overview) but i still unable to make a selection of specified term.

So the question is - how can i iterate through all terms in termset and select one (or many, depends on need) term?



GetAllTerms is a method and not property. It should be used like GetAllTerms() Following is the PowerShell Code which should work:

$siteCollectionUrl = "http://sp2010:90"
$site =new-object Microsoft.SharePoint.SPSite($siteCollectionUrl)
$session = New-Object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
$termStore = $session.TermStores[0]
$group = $termStore.Groups["Corporate Taxonomy"]
$termSet = $group.TermSets["Geography"]
$terms = $termSet.GetAllTerms()
foreach ($term in $terms)
    if ($term.Name -eq "Durban")
        Write-Host "Drban Found!"

Unfortunately, this only finds for first 'Durban' in the hierarchy. For example, if this were a 3 level hierarchy of state,county,city containing california,placer,rosevile and a illinois,snohomish,roseville, a query for roseville would only find the first roseville(seemingly randomly). To find the proper roseville in the hierarchy, one must fine the first level values, match that to california, then find the terms below california, then find placer, then look for the terms in placer to find the true roseville. Its doable in powershell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.